Closed
Description
Georg Herdt opened SPR-15266 and commented
When having bean factory methods with the same name but different arguments used for creating instance for different profiles the bean is not recognized for some profiles.
See example below:
@Configuration
public class SomeConfiguration {
@Bean(name = "something")
@Profile("A")
public String valueForTest() {
return "aaa";
}
@Bean
public String secondValue() {
return "bbb";
}
@Bean(name = "something")
@Profile("B")
@Autowired
public String valueForTest(final String secondValue) {
return secondValue;
}
}
Corresponding test will fail:
@RunWith(SpringJUnit4ClassRunner.class)
@ActiveProfiles("A")
@ContextConfiguration(classes = { SomeConfiguration.class })
public class SomeConfigurationTest_ProfileA {
@Autowired
@Qualifier("something")
private String valueForTest;
@Autowired
private String secondValue;
@Test
public void test() {
Assert.assertEquals("aaa", valueForTest);
}
}
When using profile "B" dependencies for bean named "something" will be created successfully. Providing string value "bbb".
For profile "A" it is not working properly.
Behaviour depends on used Spring version. For releases 4.0.0. up to 4.1.4 outcome will be "bbb".
I would expect "aaa".
From release 4.1.5 on (including 5.x.x releases) a NoSuchBeanDefinitionException is thrown.
See the attached gradle project to evaluate the behaviour.
Affects: 4.3.6, 5.0 M4
Attachments:
- spring-config-issue.zip (60.00 kB)
Issue Links:
- Regression in 4.1.5: Alternative @Bean declarations with same primary bean name do not work anymore [SPR-12744] #17341 Regression in 4.1.5: Alternative
@Bean
declarations with same primary bean name do not work anymore - Conditions on an overriding bean method effectively get ignored [SPR-12694] #17292 Conditions on an overriding bean method effectively get ignored
- Deterministic and JVM-independent @Bean registration order within Class-reflected configuration classes [SPR-14505] #19074 Deterministic and JVM-independent
@Bean
registration order within Class-reflected configuration classes