Announcement Announcement Module
Collapse
No announcement yet.
circular dependencies WITHIN java config Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • circular dependencies WITHIN java config

    On a similar note:

    Code:
    <bean id="a" class="TypeA">
        <property name="b" ref="b"/>
    </bean>
    
    <bean id="b" class="TypeB">
        <property name="a" ref="a"/>
    </bean>
    How might Java based configuration (@Configuration) deal with such a thing? Since java configuration pushes the construction of the bean AND the property wiring into the same java method, it seems impossible.

  • #2
    And yes, I know I can use @Autowire inside classes TypeA and TypeB. However, I have a general dislike of autowiring due to the fact that I cannot go to a 'definition' to understand what the bean is all about. In xml, I can do that. In most java config methods, I can do that, when I don't have these types of circular dependencies.

    Basically, my criticism of java config is that it's broken the separation of allocation and wiring, forcing autowire in these cases.

    The other solution, which I'm leaning towards, is refactoring to remove circular dependencies... these dependencies tend to occur most in 'utility' classes which are ripe for this sort of problem.

    Comment


    • #3
      The equilivant in JavaConfig:

      Code:
      @Configuration
      public class AppConfig
      {
        @Bean
        public TypeA typeA()
        {
          TypeA retVal = new TypeA();
          retVal.setTypeB(typeB());
          return retVal;
        }
      
        @Bean
        public TypeB typeB()
        {
          TypeB retVal = new TypeB();
          retVal.setTypeA(typeA());
          return retVal;
        }
      }

      Comment


      • #4
        mm, yes, but..

        Sure, that works until this:

        @Scope("singleton")

        on both beans. Then whomp, infinite loop.

        Comment


        • #5
          You are correct. Short of using @Autowire, I'm not sure how you'd do this using Java-based configuration. Anyone?

          Comment

          Working...
          X