以下方法怎么样:
interface Something {}
public class FirstBean implements Something {}
public class SecondBean implements Something{} // maybe empty implementation
现在配置如下:
@Configuration
public class MyConfiguration {
@Bean(name = "hello")
@ConditionalOnProperty(name = "some.property", havingValue = true)
public Something helloBean() {
return new FirstBean();
}
@Bean(name = "hello")
@ConditionalOnProperty(name = "some.property", havingValue = false)
public Something secondBean() {
return new SecondBean();
}
@Bean
@DependsOn("hello")
public MyDependantBean dependantBean() {
return new MyDependantBean();
}
}
这个想法是无论如何都要创建“Something”bean(即使它是一个空的实现),这样依赖的bean在任何情况下都会依赖于Something。
我自己没有尝试过,你知道,春天充满了魔力,但可能值得一试:)