[英]Spring Inject boolean value from property file using XML is always false
I am trying to inject boolean property from property file.我正在尝试从属性文件中注入布尔属性。 the value of the attribute is alway
false
该属性的值始终为
false
the property财产
use.virtual.wallet=true
The xml configuration xml配置
<bean id="proxyUtil" class="com.util.ProxyServiceUtility">
<property name="useVirtualWallet" value="${use.virtual.wallet}" />
</bean>
the bean豆子
public class ProxyServiceUtility {
private boolean useVirtualWallet;
public void setUseVirtualWallet(boolean useVirtualWallet) {
this.useVirtualWallet = useVirtualWallet;
}
public boolean isUseVirtualWallet() {
return useVirtualWallet;
}
}
useVirtualWallet
is alway false useVirtualWallet
总是假的
You have to load your properties file into Spring context using PropertyPlaceholderConfigurer . 您必须使用PropertyPlaceholderConfigurer 将属性文件加载到Spring上下文中。
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:com/foo/jdbc.properties"/>
</bean>
The problem fixed using this workaround, instead of injecting boolean
, I injected String
and then converted that String
to boolean
on the setter 使用此解决方法解决了问题,而不是注入
boolean
,而是注入了String
,然后在setter上将该String
转换为boolean
public void setUseVirtualWallet(String useVirtualWallet) {
this.useVirtualWallet = Boolean.parseBoolean(useVirtualWallet);
}
Another variant另一个变种
<beans
xmlns:context="http://www.springframework.org/schema/context">
<context:property-placeholder location="classpath:com/foo/jdbc.properties"/>
...
<beans>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.