2016-03-04 113 views
0

我想从外部文件夹加载所有属性文件Spring。我成功加载了一个文件,但添加通配符到混音似乎不起作用。从弹簧加载多个外部属性文件

此作品(负载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> 

这不(负载的* .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> 

错误:

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

我如何使弹簧加载文件夹中的所有外部属性文件?

编辑:我用的第一个bean(ServletContextParameterFactoryBean),因为在这个项目我检索来自web.xml文件的路径。我忘了这个,只是在bean中粘贴了路径,这是不正确的,但与问题无关。

+0

你真的有一个root/EXTERNAL_DIRECTORY吗? – 2016-03-04 12:18:33

+1

结帐这个答案http://stackoverflow.com/questions/8924912/multiple-properties-files-in-spring-3-0 – jozzy

+0

我没有'/ EXTERNAL_DIRECTORY'文件夹,它只是一个例子。 –

回答

1

尝试使用以下:

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

如果您需要包括更多的资源,你可以做下一个:

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

随着PropertyEditor默认的实现,Spring将字符串转换成Resource。你可以找到details in documentation

希望这会有所帮助。

+0

'地点'属性工作!非常感谢:) –

+0

不客气。 –