Announcement Announcement Module
Collapse

JavaConfig forum decommissioned in favor of Core Container

As described at

http://static.springsource.org/sprin...fig/README.TXT

key features of the Spring JavaConfig project have been migrated into the core Spring Framework as of version 3.0.

Please see the Spring 3.0 documentation on @Configuration and @Bean support:

http://static.springsource.org/sprin...tml#beans-java

For any questions related to @Configuration classes and @Bean methods in Spring 3.0, please post in the dedicated 'Core Container' forum at

http://forum.springsource.org/forumdisplay.php?f=26
See more
See less
Using JavaConfig in another JavaConfig Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Using JavaConfig in another JavaConfig

    Dear community!

    I'm absolutely new to spring and i would like to do Dependency Injection
    with Spring using JavaConfig.

    I would like to have 2 configuration classes:

    @Configuration
    public class SpringConfiguration1 {
    @Bean
    public Foo foo() {
    return new Foo();
    }

    }

    @Configuration
    public class SpringConfiguration2 {

    @Bean
    public Bar bar() {
    SpringConfiguration config = new SpringConfiguration();
    Bar bar = new Bar();
    bar.setFoo(config.foo());
    return bar;
    }
    }

    So i have 2 configuration, with the second configuration using
    the bean foo of the first one.

    I would like to have multiple configuration classes because i would like
    to separate the setup of different components of our system.

    Is the above way correct to achieve this - or is there a completely
    different way to achieve this?

    Thanks for you help,
    Dirk

  • #2
    Hi Dirk,

    There are several approaches to accomplish what you want.

    1) Use @Autowired to inject one @Configuration instance into another

    Code:
    @Configuration
    public class SpringConfiguration1 {	
        @Bean
        public Foo foo() {
            return new Foo();
        }
    }
    
    @Configuration
    public class SpringConfiguration2 {
        @Autowired SpringConfiguration1 config;
    
        @Bean
        public Bar bar() {
            Bar bar = new Bar();
            bar.setFoo(config.foo());
            return bar;
        }
    }
    The above assumes that both SpringConfiguration1 and SpringConfiguration2 are registered as beans with the enclosing ApplicationContext. This could have happened either by explicitly declaring them with <bean/> in the XML, or via component-scanning. The style above is nice because it provides full IDE navigability between @Configuration classes.

    2) Use @Autowired to inject a @Bean declared in another @Configuration class

    Code:
    @Configuration
    public class SpringConfiguration1 {	
        @Bean
        public Foo foo() {
            return new Foo();
        }
    }
    
    @Configuration
    public class SpringConfiguration2 {
        @Autowired Foo foo;
    
        @Bean
        public Bar bar() {
            Bar bar = new Bar();
            bar.setFoo(foo);
            return bar;
        }
    }
    Again, this approach assumes that both @Configuration classes have been registered as beans with the ApplicationContext. This style's strength is that SpringConfiguration2 is decoupled from SpringConfiguration1. It has no idea where the Foo bean is coming from. The downside is that it's harder to discover in the IDE (although the latest release of the SpringSource Tool Suite helps with this!)

    3) Use @Import

    Code:
    @Configuration
    public class SpringConfiguration1 {	
        @Bean
        public Foo foo() {
            return new Foo();
        }
    }
    
    @Configuration
    @Import(SpringConfiguration1.class)
    public class SpringConfiguration2 {
        @Autowired Foo foo;
        // OR
        @Autowired SpringConfiguration1 config;
    
    
        @Bean
        public Bar bar() {
            Bar bar = new Bar();
            bar.setFoo(foo);
            // OR
            bar.setFoo(config.foo());
            return bar;
        }
    }
    As you can see, the style above is very similar to the two above it. The only difference is the use of @Import. Using @Import, only SpringConfiguration2 needs to ever have been registered as a bean with the ApplicationContext. Upon being processed, it will 'import' SpringConfiguration1. This is very similar to the way that <import/> works in Spring XML. The strength here is that you can aggregate many @Configuration classes together without having to specify all of them in XML (or component-scan for them). The downside is tight coupling between the importing and imported @Configuration classes.


    I hope that gives you everything you need!

    Comment


    • #3
      Great answer. Thanks very much, Chris! That really helped!

      Comment

      Working...
      X