You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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...
From my perspective, the proper solution here is to declare BeanFactoryPostProcessor (and also BeanPostProcessor) definitions as static@Bean methods, not triggering the instantiation of the containing configuration class to begin with. I guess we need to add a stronger note on that. Maybe we should even log a warning (or info at least) when a post-processor bean is being created through a non-static factory method...
Uh oh!
There was an error while loading. Please reload this page.
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:
In this case, the value is not injected into the field because
fooConfig
is created before theBPP
s...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:
@Configuration
classes@Bean
methods cannot refer to each other with Spring scoping semanticsReferenced from: commits 90493f4
The text was updated successfully, but these errors were encountered: