Closed
Description
Stepan Koltsov opened SPR-8525 and commented
public class QuietlyOverrides {
@Configuration
public static class Conf1 {
@Bean
public Inflater inflater() {
System.out.println("1");
return new Inflater();
}
}
@Configuration
public static class Conf2 {
@Bean
public Inflater inflater() {
System.out.println("2");
return new Inflater();
}
}
@Configuration
@Import({ Conf1.class, Conf2.class })
public static class Conf {
}
public static void main(String[] args) {
// expecting an exception // Spring quietly creates only second bean new AnnotationConfigApplicationContext(Conf.class);
System.out.println("$");
}
} //~
We have large application, and one developer accidentally created a bean with name same as name of bean in another part of application. Problem search was not easy.
I think Spring should throw an error if independent configurations declare beans with same name. Configurations are independent if neither of which include another configuration directly or indirectly.
However, IF Conf2 includes Conf1, Spring should quietly override bean declared in Conf1 with bean declared if Conf2, as it does now. It is convenient to "inherit" configuration and override some bean definitions.
Affects: 3.1 M2