我有2个配置文件如下:
Config1:
@Configuration
public class Config1 {
@Bean
@Order(1)
public String example() {
return new String("example1");
}
}
配置2:
@Configuration
public class Config2 {
@Bean
@Order(2)
public String example() {
return new String("example2");
}
}
我的测试类,配置为:
@Configuration
@Import({Config1.class, Config2.class})
public class TestApp {
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(TestApp.class);
System.out.println("Bean By Name:: " + ctx.getBean("example", String.class));
}
}
运行上面的代码后,我得到了:
Bean By Name :: example2
而不是获取(因为配置1在@Order注释中具有较低的数字):
Bean By Name :: example1
但是,当我将所有bean放在Config1中并保持@Order编号时,我得到了预期的结果:
Bean By Name :: example1
这种行为的解释是什么?