Results 1 to 5 of 5

Thread: circular dependencies WITHIN java config

  1. #1

    Default 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.
    some clever-ish quote goes here...

  2. #2

    Default

    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.
    some clever-ish quote goes here...

  3. #3
    Join Date
    Jul 2010
    Posts
    139

    Default

    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;
      }
    }

  4. #4

    Default mm, yes, but..

    Sure, that works until this:

    @Scope("singleton")

    on both beans. Then whomp, infinite loop.
    some clever-ish quote goes here...

  5. #5
    Join Date
    Jul 2010
    Posts
    139

    Default

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

Posting Permissions

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