简体   繁体   English

从Spring加载多个外部属性文件

[英]Loading multiple external properties files from spring

I want to load all the properties files with Spring from an external folder. 我想从外部文件夹使用Spring加载所有属性文件。 I successfully load one file but adding a wildcard to the mix does not seem to work. 我成功加载了一个文件,但是将通配符添加到组合中似乎不起作用。

This works (load test.properties ): 这有效(加载test.properties ):

<bean id="propertiesLocation"
    class="org.springframework.web.context.support.ServletContextParameterFactoryBean">
    <property name="initParamName"><value>file://EXTERNAL_DIRECTORY/test.properties</value></property>
</bean>

<bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" ref="propertiesLocation"></property>
    <property name="ignoreUnresolvablePlaceholders" value="true" />
    <property name="order" value="0"/>
</bean>

This does not (load *.properties ): 这不会(加载* .properties ):

<bean id="propertiesLocation"
    class="org.springframework.web.context.support.ServletContextParameterFactoryBean">
    <property name="initParamName"><value>file://EXTERNAL_DIRECTORY/*.properties</value></property>
</bean>

<bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" ref="propertiesLocation"></property>
    <property name="ignoreUnresolvablePlaceholders" value="true" />
    <property name="order" value="0"/>
</bean>

The error: 错误:

Caused by: java.io.FileNotFoundException: /EXTERNAL_DIRECTORY/*.properties (No es un directorio)

How can I make Spring load all the external properties files in a folder? 如何使Spring加载文件夹中的所有外部属性文件?

Edit : I use the first bean ( ServletContextParameterFactoryBean ) because in the project I retrieve the path from the web.xml file. 编辑 :我使用第一个bean( ServletContextParameterFactoryBean ),因为在项目中我从web.xml文件中检索路径。 I forgot about this and just pasted the path in the bean, it is incorrect but has nothing to do with the question. 我忘记了这一点,只是将路径粘贴到Bean中,这是不正确的,但与问题无关。

Try use following: 尝试使用以下内容:

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations" value="file://EXTERNAL_DIRECTORY/*.properties"/>
    <property name="ignoreUnresolvablePlaceholders" value="true" />
    <property name="order" value="0"/>
</bean>

If you need include more resources you can do next: 如果需要包括更多资源,则可以执行以下操作:

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations" >
        <list>
            <value>classpath:single.properties"</value>
            <value>file://EXTERNAL_DIRECTORY/*.properties"</value>
            <value>file://ANOTHER_EXTERNAL_DIRECTORY/*.properties"</value>
        </list>
    </property>
    <property name="ignoreUnresolvablePlaceholders" value="true" />
    <property name="order" value="0"/>
</bean>

With default implementation of PropertyEditor , Spring will convert strings into Resource . 使用PropertyEditor默认实现,Spring会将字符串转换为Resource You can find details in documentation . 您可以在文档中找到详细信息

Hope this will be helpful. 希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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