-
Couldn't load subscription status.
- Fork 38.8k
Description
Gary Russell opened SPR-13285 and commented
While the new documentation now warns that factory-method injection is preferred to auto wiring, there are cases where auto wiring does not work at all with @Configuration classes.
Consider:
@Configuration
public class FooConfig {
@Value("${foo}")
public String foo;
@Bean
public String earlyFoo() {
return this.foo;
}
@Bean
public String foo(@Value("${foo}") String foo) {
return foo;
}
@Bean
public PropertySourcesPlaceholderConfigurer configurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}
public class Testing {
@Test
public void test() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(FooConfig.class);
StandardEnvironment env = new StandardEnvironment();
Properties props = new Properties();
props.setProperty("foo", "bar");
PropertiesPropertySource pps = new PropertiesPropertySource("sftpprop", props);
env.getPropertySources().addLast(pps);
context.setEnvironment(env);
context.refresh();
assertNull(context.getBean("earlyFoo"));
assertEquals("bar", context.getBean("foo"));
context.close();
}
}
In this case, the value is not injected into the field because fooConfig is created before the BPP s...
2015-07-28 14:39:08,114 [main] DEBUG: org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'fooConfig'
...
2015-07-28 14:39:08,130 [main] DEBUG: org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
I don't know if this can be fixed, but perhaps we should document the conditions under which it occurs.
Affects: 4.2 RC3
Issue Links:
- Documentation the limitation of injecting components in @Configuration classes [SPR-12773] #17370 Documentation the limitation of injecting components in
@Configurationclasses - Doc: Static @Bean methods cannot refer to each other with Spring scoping semantics [SPR-13118] #17709 Doc: Static
@Beanmethods cannot refer to each other with Spring scoping semantics
Referenced from: commits 90493f4