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
Autowire and qualifiers for bean creation nmethods? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Autowire and qualifiers for bean creation nmethods?

    I see that a whole slew of issues have been deferred (eg, SJC-51, 62). Are there any plans to implement these? Are there technical blockers? This would seem to be major feature gap in javaconfig. Well, at least its fully blocking us from using javaconfig as we rely heavily on autowire and qualifiers.

  • #2
    Hi Barry,

    SJC-62 is actually resolved. I wonder if you're referring to SJC-50 and SJC-51, both of which deal with allowing parameters to @Bean methods, and both of which are deferred?

    Technically speaking, it's possible to allow autowired parameters to @Bean methods - it was actually implemented for a short time. The reason this functionality has been removed is that it doesn't quite make sense. If a @Bean method accepts parameters, it prevents any other @Bean method from referring to it. Consider the following example:

    Code:
    @Configuration
    public class AppConfig {
        // DataSource parameter is 'autowired'
        public @Bean AccountRepository accountRepository(DataSource dataSource) {
            return new JdbcAccountRepository(dataSource);
        }
    
        public @Bean TransferService transferService() {
            return new TransferService(accountRepository(/* uh-oh! */));
        }
    }
    Notice that transferService() cannot successfully call accountRepository()? It would have to pass along a dataSource parameter as well. This inability for one @Bean method to easily reference another @Bean method violates JavaConfig's basic model for dependency injection. Therefore, we've disallowed @Bean parameters.

    The good news is that field autowiring is fully supported in @Configuration classes:

    Code:
    @Configuration
    @AnnotationDrivenConfig
    public class AppConfig {
        @Autowired
        private DataSource dataSource;
    
        public @Bean AccountRepository accountRepository() {
            return new JdbcAccountRepository(dataSource);
        }
    
        public @Bean TransferService transferService() {
            return new TransferService(accountRepository());
        }
    }
    The above works without a problem. Note that support for autowiring (@AnnotationDrivenConfig, @ComponentScan) is fully documented. See the M4 reference documentation for details.

    Comment


    • #3
      Yes, I see that having arguments on @Bean methods doesn't make sense. Does the field level autowiring support qualifiers? I didn't see anything in the docs.

      BTW Chris, java-config is really starting to kick ass. We are looking forward to a wholesale dumping of xml-config. Thanks!

      Comment


      • #4
        Originally posted by memelet View Post
        Yes, I see that having arguments on @Bean methods doesn't make sense. Does the field level autowiring support qualifiers? I didn't see anything in the docs.
        Yep. @Qualifier works.

        Originally posted by memelet View Post
        BTW Chris, java-config is really starting to kick ass. We are looking forward to a wholesale dumping of xml-config. Thanks!
        Good to hear, and keep the feedback coming - it makes all the difference!

        Comment

        Working...
        X