Closed
Description
Rob Winch opened SPR-12646 and commented
Starting with Spring 4.0.7 (4.0.6 works) the following setup will fail because only the ToAutoWire("a") will be Autowired
:
public class ToAutoWire {
private final String name;
public ToAutoWire(String name) {
super();
this.name = name;
}
}
@Configuration
public class ConfigA {
@Bean
public ToAutoWire a() {
return new ToAutoWire("a");
}
}
@Configuration
public class ConfigB {
List<ToAutoWire> values;
@Bean
public ToAutoWire b() {
return new ToAutoWire("b");
}
@Autowired
public void setValues(List<ToAutoWire> values) {
this.values = values;
}
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {ConfigA.class, ConfigB.class})
public class AutowireListTests {
@Autowired
ConfigB b;
@Test
public void run() {
assertEquals(2,b.values.size());
}
}
A few notes:
- Using Spring 4.0.6 and prior appears to work just fine
- If ConfigA is removed from the test,
ConfigB
will correctly autowireToAutoWire("b")
- When I was putting this together I got this isolated test case within another project and tried to reproduce it, but it failed. For whatever reason coping the complete project exactly as is and then removing the additional pieces seemed to ensure the issue was fixed in 4.0.6 properly (it was consistently failing otherwise). This leads me to believe this may be some sort of ordering issue.
- You can find a sample of the application at https://github.com/rwinch/spring-framework-issues/tree/SPR-12646/SPR-12646 (Note this is in my @Autowired List<ToAutoWire> into Configuration that defines @Bean ToAutoWire fails [SPR-12646] #17247 branch)
- This seems to be causing SEC-2822
Affects: 4.0.7, 4.0.8, 4.0.9, 4.1 GA, 4.1.1, 4.1.2, 4.1.3, 4.1.4
Reference URL: https://github.com/rwinch/spring-framework-issues/tree/SPR-12646
Issue Links:
- Autowire bean in @Configuration class itself [SPR-12127] #16743 Autowire bean in
@Configuration
class itself - Spring may invoke @Bean methods too early in case of a circular reference [SPR-12018] #16634 Spring may invoke
@Bean
methods too early in case of a circular reference - SEC-2822 IllegalStateException: Cannot apply DaoAuthenticationConfigurer to already built object