如何在Spring中为属性文件位置加载系统属性

时间:2016-12-02 10:29:17

标签: java spring jboss

我正在开发一个Spring应用程序,其中属性文件将打包在.war文件中以进行部署。

<context:property-placeholder location="classpath:application.properties" />

但是,我希望能够使用另一个可以在standalone.xml中指定为系统属性的文件覆盖它们:

</extensions>

<system-properties>
    <property name="CONFIG_FILE_LOCATION" value="/path/to/application.properties"/>
</system-properties>

这是我的解决方案,

<context:property-placeholder location="classpath:application.properties,
                                        file:///${CONFIG_FILE_LOCATION}" />

但显然Spring无法找到它

Caused by: java.io.FileNotFoundException: ${CONFIG_FILE_LOCATION} (The system cannot find the file specified)

有谁知道我怎么解决它?有没有其他方式Spring访问系统属性?

2 个答案:

答案 0 :(得分:1)

实际上,Spring可以通过使用此解决方案为另一个文件指定system property位置来覆盖某些属性:

<context:property-placeholder location="classpath:alarm_notification.properties, file:///${CONFIG_FILE_LOCATION}" />

如果某个属性未在位于CONFIG_FILE_LOCATION的文件中被覆盖,则会使用application.properties中的值。

只需确保在用于启动服务器的standalone.bat文件中进行以下配置:

</extensions>

<system-properties>
    <property name="CONFIG_FILE_LOCATION" value="/path/to/application.properties"/>
</system-properties>

答案 1 :(得分:0)

您需要为其命名命名空间,如下所示:

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
    <list>
        <value>file:/myFolder/folder/path/application.properites</value>
    </list>
</property>

或如下:

<context:property-placeholder locations="file:/myFolder/folder/path/application.properites"/> 
相关问题