Announcement Announcement Module
Collapse
No announcement yet.
Java Beans Configuration Best Practices Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Java Beans Configuration Best Practices

    Hello guys,

    I like the new Java Beans Config and used it in a simple project already. Now I'm trying to use that configuration style in a bigger project. I tried to use a custom FactoryBean and stumbled across two alternatives for expressing dependencies between beans. Imaging the following sample:

    Code:
    @Configuration
    @Import(DaoConfiguration.class)
    public class Configuration {
      @Autowired
      private DaoInterface dao
    
      @Bean
      public Service myService() {
        return new MyService(this.dao);
      }
    }
    This is the style used in the spring documentation. Nevertheless I've also seen a blog post and forum posts using the following style:

    Code:
    @Configuration
    @Import(DaoConfiguration.class)
    public class Configuration {
      @Bean
      public Service myService(final DaoInterface dao) {
        return new MyService(dao);
      }
    }
    I tried it and it works like a charm! But I didn't find any comment about that style in the spring reference documentation. I'm wondering whether this approach has any draw backs or possible pitfalls? What is the recommended approach for expressing dependencies using java beans config?


    One benefit of the second approach is the support for FactoryBeans! Imagine the following DaoConfiguration:

    Code:
    @Configuration
    public class DaoConfiguration {
    
      @Bean
      public MyFactoryBean dao() {
        MyFactoryBean factory = new MyFactoryBean(); // implements Springs FactoryBean
        factory.forDao(DaoInterface.class);
        return factory; 
      }
    }
    The DaoConfiguration above does work with the second Configuarion implementation, but not with the first one. So why is the argument-style of expressing dependencies not explained in Spring's reference documentation? What are the best practices using java beans config? Which style of configuration should be used?

    Thanks for any comments,
    Kyle

  • #2
    I would just stick with field injection instead of method parameter injection.

    At work, I once ran into a weird interaction between providers, scopes, and qualifiers that broke wiring. It was resolved by changing to field injection.

    Here's the smallest example I can find that demonstrates the broken behavior as of 3.1.1. I believe it started occurring in 3.1.0.

    Code:
      public static void main(String[] args) {
        ApplicationContext c = new AnnotationConfigApplicationContext(NeedsAConfig.class, AConfig.class, BConfig.class);
        c.getBean("needsA");
      }
    
      @Configuration
      static class NeedsAConfig {
        @Bean
        Object needsA(@Qualifier("a") Provider<Object> aProvider) {
          aProvider.get();
          aProvider.get();
          return new Object();
        }
      }
    
      @Configuration
      static class AConfig {
        @Bean
        @Scope(BeanDefinition.SCOPE_PROTOTYPE)
        Object a(@Qualifier("b") Provider<Object> bProvider) {
          bProvider.get();
          return new Object();
        }
      }
    
      @Configuration
      static class BConfig {
        @Bean
        Object b() {
          return new Object();
        }
      }
    Changing AConfig to this will work:
    Code:
      @Configuration
      static class AConfig {
        @Autowired
        @Qualifier("b")
        Provider<Object> bProvider;
    
        @Bean
        @Scope(BeanDefinition.SCOPE_PROTOTYPE)
        Object a() {
          bProvider.get();
          return new Object();
        }
      }
    Last edited by slow; May 2nd, 2012, 11:40 AM.

    Comment

    Working...
    X