在通过扩展原始 @Configuration 类覆盖 Javaconfig Bean 时,我想为新的 Bean 定义添加一个 @DependsOn。
但是,这种依赖似乎没有被考虑在内。这是一个重现我的问题的TestCase:
public class SpringTest {
@Test
public void testDependsOnTakenInAccount() {
AnnotationConfigApplicationContext ctx2 = new AnnotationConfigApplicationContext(AConfig.class, CConfig.class);
Assert.assertEquals("overriden", ctx2.getBean("bean"));
}
@Configuration
public static class AConfig {
@Bean
public Object bean() {
return "not overriden";
}
}
@Configuration
public static class CConfig extends AConfig {
protected boolean isInitialized = false;
@Bean
public Void doInit() {
isInitialized = true;
return null;
}
@Bean
@DependsOn("doInit")
public Object bean() {
if (!isInitialized) {
throw new RuntimeException("Not initialized");
}
return "overriden";
}
}
}
这是预期的行为吗?如果是,如何在覆盖 bean 时添加依赖项?