Results 1 to 4 of 4

Thread: Autowire and qualifiers for bean creation nmethods?

  1. #1
    Join Date
    Aug 2004
    Posts
    230

    Default 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.
    Barry Kaplan (memelet)

  2. #2
    Join Date
    Apr 2007
    Posts
    307

    Default

    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.
    Chris Beams
    Spring Framework committer, VMware
    http://github.com/cbeams

  3. #3
    Join Date
    Aug 2004
    Posts
    230

    Default

    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!
    Barry Kaplan (memelet)

  4. #4
    Join Date
    Apr 2007
    Posts
    307

    Default

    Quote 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.

    Quote 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!
    Chris Beams
    Spring Framework committer, VMware
    http://github.com/cbeams

Posting Permissions

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