【发布时间】:2018-07-22 10:22:48
【问题描述】:
我想以这样一种方式配置 spring bean,即根据布尔变量的值,两个可用的连接 bean 之一在代码中自动装配。
下面是布尔变量的初始化:
//This is overridden as false from the properties file on the server.
@Value(value = "${my.property.connectionOne:true}")
private boolean connectionOne;
我是这样定义 Bean 的:
@Bean(name = "specificConnection")
public Destination getSpecificConnection() throws Exception {
if (connectionOne) { //boolean variable
return new ConnectionOne("DB");
}
else {
return new ConnectionTwo("XML");
}
}
其中ConnectionOne 和ConnectionTwo 都实现了Destination
我正在使用所需类中的 bean:
@Autowired
@Qualifier(value = "specificConnection")
private Destination specificConnection;
但是,它似乎不起作用。即使我将布尔变量的值更改为 false,它也会一直返回 ConnectionOne。
我使用的是 Spring 4.2.0 版和 Wildfly 服务器。
如果需要进一步说明,请告诉我。
【问题讨论】:
-
显示connectionOne的初始化位置
-
你确定connectionOne布尔值是假的吗?
-
connectionOne 变量的定义位置以及更改值的位置
-
是的@Betlista,该属性已从服务器属性文件中覆盖。 @АлмасАбдразак 请看下面 //这是从服务器上的属性文件中覆盖的。
@Value(value = "${my.property.connectionOne:true}") private boolean connectionOne; -
@АлмасАбдразак 编辑了我的原始代码以显示声明,因为注释的格式不正确。