简体   繁体   中英

How to read a list from a properties file?

In my abc.properties file I have a list

xyz=cat,dog,cow,calf

I want to read that from my java code.

I tried @Value annotation

 @Value("${xyz}")  private String[] elementToSearch;

But clearly I am doing something wrong because when I print elementToSearch[0] I get ${xyz} Any help appreciated.

It looks like you're trying to use the @Value annotation from the Spring framework.

Actually, I think you're doing it right. Is possible you don't have a PropertyPlaceHolderConfigurer in play?

Most of the time I've seen it done by adding something like this to your Spring applicationContext.xml configuration file:

    <!-- if you are using annotations -->
    <context:annotation-config/>

    <!-- if you are scanning for annotated beans -->
<context:component-scan base-package="com.example.package" />


<!-- Load override configuration from a property file. --> 
<bean id="propertyConfigurer"  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
    <property name="ignoreResourceNotFound" value="true"/>
    <property name="locations">
        <list>
            <value>classpath:abc.properties</value>
                <value>file:/etc/someplace/abc.properties</value>
        </list>
    </property>
</bean>

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