Announcement Announcement Module
Collapse
No announcement yet.
Circular Reference Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    [filepath)/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
    I made a little test with your configuration and it works for me. What version of Spring are you using ?

    Comment


    • #3
      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, 07:10 AM.

      Comment


      • #4
        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

        Comment


        • #5
          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, 07:10 AM.

          Comment


          • #6
            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.

            Comment


            • #7
              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).

              Comment


              • #8
                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.

                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, 07:09 AM.

                Comment

                Working...
                X