简体   繁体   中英

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

the property

use.virtual.wallet=true

The xml configuration

<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

You have to load your properties file into Spring context using PropertyPlaceholderConfigurer .

<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

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>
 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM