Results 1 to 8 of 8

Thread: Cyclic references in context xml

  1. #1

    Default Cyclic references in context xml

    Hi All!

    When writing a Spring context, are cyclic references handled?

    For example,

    Java:
    A a;
    B b;
    a.b = b;
    b.a = a;

    Spring context:
    <bean id="a">
    <property name="b"><ref local="b"/></property>
    </bean>

    <bean id="b">
    <property name="a"><ref local="a"/></property>
    </bean>

    If they are not handled, is there a workaround to allow this setup?

    Thanks for any advice!

    Rob

  2. #2
    Join Date
    May 2005
    Location
    Paris - France
    Posts
    5

    Default Re : Cyclic references in context xml

    Hello Rob,

    you need to create a third class, let's call it C, that uses both class A and B. Then you just have to use C and you can break the cyclic reference between A and B.

    Java :
    A a;
    B b;
    C c;

    C.a = a;
    C.b = b;

    C.doSomethingWithAandB(...);

    Hope this help,

    Vincent

  3. #3
    Join Date
    Jun 2007
    Posts
    4

    Default

    Exactly the question I was right now looking an answer for [i know topic is old]

    I have an 'email' service and an 'errors' service. 'email' can post smtp errors to the 'errors' service, but 'errors' service can send various other errors via email. So I was wondering about the cyclic references.

    As right now I'm still wondering if and what the class 'C' might be, I still find one of the question unanswered.
    Are cyclic references handled by Spring or not ?

  4. #4
    Join Date
    Mar 2007
    Posts
    515

    Default

    http://static.springframework.org/sp...ctor-injection
    there is a small text-box called "Circular dependencies".

    If you have two singleton beans that reference each other, using setter injection then it will work. If those beans are prototypes (non-singleton), then you'll end up with a circular dependency issue.

  5. #5
    Join Date
    Mar 2007
    Posts
    515

    Default

    Here's another good explanation for other case of circular dependencies.

  6. #6
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    As Andrei said with setter injection, you might get away with it. I've found Spring to be darn clever in the past at resolving problems like this.
    Last edited by karldmoore; Aug 27th, 2007 at 03:30 PM.
    Barracuda Networks SSL VPN Lead Developer
    http://pramatr.wordpress.com
    http://twitter.com/karldmoore
    http://www.linkedin.com/in/karldmoore
    Any postings are my own opinion, and should not be attributed to my employer or clients.

  7. #7
    Join Date
    Jun 2007
    Posts
    4

    Default

    Thanks Andrei, for pointing out those links, you are right. I do not use constructors and application behaves properly.

    Basically I did circular dependencies with setter injection and it worked, but now, trying to proxy the services (so I can have transactions and OSIV) made me think twice whether this is a good pattern and sought help.

    Spring is smart indeed

  8. #8
    Join Date
    Jun 2007
    Posts
    5

    Default

    Try this link for a solution to circular dependencies, using a custom injector with annotations http://forum.springframework.org/showthread.php?t=41138&highlight=circular+dependen cy

Posting Permissions

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