如何从属性文件中读取列表?

b5buobof  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(190)

在我的 .properties 我有一份清单

xyz=cat,dog,cow,calf

我想从我的java代码中读一下。
我试过了 @Value 注解

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

但很明显我做错了什么因为当我打印的时候 elementToSearch[0] 我明白了 ${xyz} 谢谢你的帮助。

vm0i2vca

vm0i2vca1#

看起来您正在尝试使用spring框架中的@value注解。
实际上,我认为你做得对。有没有可能你没有一个PropertyPlaceHolderConfigure?
大多数情况下,我都是通过向spring applicationcontext.xml配置文件中添加这样的内容来完成的:

<!-- 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>

相关问题