Announcement Announcement Module
Collapse
No announcement yet.
Inconsistent @Autowired behavior for unqualified field Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Inconsistent @Autowired behavior for unqualified field

    Am I missing something here? This occurs in 3.0.x and 3.1.x.

    Base providing Config
    Code:
      @Configuration
      class FooConfig {
        @Bean
        String foo() {
          return "foo";
        }
    
        @Bean
        String baz() {
          return "baz";
        }
      }
    Various cases paired with above base config
    Code:
      @Configuration
      class Case0Config {
        @Autowired
        String foo2;
    
        @Bean
        String foobar() {
          return foo2 + "bar";
        }
      }
      
      @Configuration
      class Case1Config {
        @Autowired
        String foo;
    
        @Bean
        String foobar() {
          return foo + "bar";
        }
      }
      
      @Configuration
      class Case2Config {
        @Autowired 
        @Qualifier("foo")
        String foo;
        
        @Bean
        String foobar() {
          return foo + "bar";
        }
      }
      
      @Configuration
      class Case3Config {
        @Bean
        String foobar(String foo) {
          return foo + "bar";
        }
      }
    Runner
    Code:
     public static void main(String[] args) {
        // new AnnotationConfigApplicationContext(FooConfig.class, Case0Config.class);
        // above will fail as expected since field name is different
        
        ApplicationContext c1 = new AnnotationConfigApplicationContext(FooConfig.class, Case1Config.class);
        System.out.println(c1.getBean("foobar")); 
        // nullbar  <- the inconsistency
        
        ApplicationContext c2 = new AnnotationConfigApplicationContext(FooConfig.class, Case2Config.class);
        System.out.println(c2.getBean("foobar"));
        // foobar
        
        ApplicationContext c3 = new AnnotationConfigApplicationContext(FooConfig.class, Case3Config.class);
        System.out.println(c3.getBean("foobar")); 
        // foobar
      }
Working...
X