Results 1 to 8 of 8

Thread: Circular Reference

  1. #1
    Join Date
    Nov 2006
    Posts
    7

    Default Circular Reference

    Hello,

    I am using autowire by-name and setter injection.

    If I declare a bean in the following way:
    <bean id="xService" class="a.b.c.XServiceBean"/>
    the app deploys correctly (on JBoss).

    If (for a certain reason) I try to do the following:

    <bean id="xServiceFactory"
    class="org.springframework.beans.factory.config.Ob jectFactoryCreatingFactoryBean">
    <property name="targetBeanName" value="xServiceBean"/>
    </bean>

    <bean id="xService"
    factory-method="getObject"
    factory-bean="xServiceFactory"
    />

    <bean id="xServiceBean" class="a.b.c.XServiceBean"/>
    The app does not deploy because of a circular reference, and here is the exception trace:


    Error creating bean with name 'xService' defined in URL
    [file:(path)/someSpringConfig.xml]: Instantiation of bean failed; nested
    exception is org.springframework.beans.factory.BeanDefinitionSt oreException :
    Factory method [public java.lang.Object
    org.springframework.beans.factory.config.ObjectFac toryCreatingFactoryBean$1.getObject()
    throws org.springframework.beans.BeansException] threw exception; nested
    exception is org.springframework.beans.factory.BeanCurrentlyInC reationException
    : Error creating bean with name 'Singleton 'xService' is already in creation':
    Requested bean is currently in creation: Is there an unresolvable circular
    reference?

    I would like to go for the solution with the ObjectFactoryCreatingFactoryBean,
    I'm looking for ideas as to what could I try to fix the circular reference problem.

    Thanks,
    Zubrowka

  2. #2
    Join Date
    Mar 2007
    Posts
    515

    Default

    I made a little test with your configuration and it works for me. What version of Spring are you using ?

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

    Default

    Also is there any more configuration here that could be causing this? Is it possible to see all of the configuration?
    Last edited by karldmoore; Aug 30th, 2007 at 07:10 AM.
    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.

  4. #4
    Join Date
    Nov 2006
    Posts
    7

    Post

    I cannot post the full configuration as it's a part of quite a large enterprise system but I suppose the problem occurs because the XServiceBean class contains a reference to a different service, say YService. YServiceBean, which implements YService, has a reference to XService:
    class XServiceBean {
    YService yService;
    ... (rest of class body, setter for yService)
    }

    class YServiceBean {
    XService xService;
    ... (rest of class body, setter for xService)
    }

    In my spring configuration file YService is configured this way:
    <bean id="yService" class="a.b.c.YServiceBean"/>
    The problem is I prefer not to refactor this code (I'm aware that it's not an elegant solution) and there may be other circular references in the code.

    Thanks,
    Zubrowka

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

    Default

    If there are circular references, I'm not sure how much you can work around the problem. Spring does try and let you get away with quite a lot, and favouring setters over constructors will help. Apart from that.............
    Last edited by karldmoore; Aug 30th, 2007 at 07:10 AM.
    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.

  6. #6
    Join Date
    Jun 2005
    Posts
    4,230

    Default

    I would hazard a guess that the missing configuration contains some proxy definitions or auto-proxying. Circular references are almost always resolvable *unless* proxies are being created at critical points in the cycle (hence Andrei is probably correct when he says that as it stands everything works). Actually the most common cause is proxies being created for all beans in the cycle, and with quite a small cycle. I think sometimes the framework could do more to help with this, but I'm pretty sure it's not an easy problem to solve.

    The only ways I know of to resolve it is to either redesign your object graph to cut out one or two cycles, or to manually take control of one of the dependencies in the chain and resolve it through an explicit lookup, or to be more careful with pointcuts and make sure that not everything in the cycle is proxied. Yuck.

  7. #7
    Join Date
    Jun 2005
    Posts
    4,230

    Default

    Although I have to say, on reading the configuration carefully, I can't see any cycles at all. So something even more important is missing (possibly part of my brain).

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

    Default

    Quote Originally Posted by Dave Syer View Post
    I would hazard a guess that the missing configuration contains some proxy definitions or auto-proxying. Circular references are almost always resolvable *unless* proxies are being created at critical points in the cycle (hence Andrei is probably correct when he says that as it stands everything works).
    I've never actually tried to break it, I'm quite interested in how much Spring lets you get away with in this respect.

    Quote Originally Posted by Dave Syer View Post
    Although I have to say, on reading the configuration carefully, I can't see any cycles at all. So something even more important is missing (possibly part of my brain).
    Last edited by karldmoore; Aug 30th, 2007 at 07:09 AM.
    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.

Posting Permissions

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