Results 1 to 2 of 2

Thread: Cycle handling

  1. #1

    Default Cycle handling

    What is the best way to making two prototype beans aware of each other?

    Clearly cycles in a bean dependency graph won't work:
    Code:
    <beans>
    	<bean id="a" class="RefBean1"	singleton="false">
    		<property name="ref"><ref bean="b"/></property>
    	</bean>
    	<bean id="b" class="RefBean2"	singleton="false">
    		<property name="ref"><ref bean="a"/></property>
    	</bean>	
    </beans>
    causes Spring to loop until out of memory.

    Defining :

    Code:
    <bean id="b" class="RefBean2"	singleton="false">
       <lookup-method name="lookupA"   bean="a"/>
    </bean>
    gives the same problem.

    Presumably this is because Spring tries to build a chain of instances A->B->A->B... .

    What I really want is A<=>B, i.e. A is supplied with a B instance and the B instance is made aware of the A instance.

    What is the best approach to handling this? Can this be done without making one of the beans a Singleton?

  2. #2
    Join Date
    Aug 2004
    Location
    Sydney
    Posts
    503

    Default

    Could you write 2 simple FactoryBeans? (org.springframework.beans.factory.FactoryBean)

    Your FactoryBeans could take care of doing the A.set(B), and B.set(A) before returning a properly initialised A or B. They'd also need to continue to make sure that new RefBean1 and RefBean2 were used (just looking at the fact you're using prototypes).

    One FactoryBean would return an initialised RefBean1 and the other a RefBean2

    If your beans weren't prototypes, you could otherwise look at using a BeanFactoryPostProcessor to set the cyclic references after Spring had initialised.

Similar Threads

  1. Managing components / projects / dev cycle: Maven or ant?
    By rebornspirit in forum Architecture
    Replies: 9
    Last Post: Oct 6th, 2006, 09:34 AM
  2. Replies: 1
    Last Post: Sep 20th, 2005, 09:14 PM
  3. Replies: 2
    Last Post: Sep 20th, 2005, 12:59 AM
  4. Replies: 0
    Last Post: May 27th, 2005, 09:21 AM
  5. Replies: 8
    Last Post: Mar 23rd, 2005, 02:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •