2016-12-02 66 views
2

我正在开发一个Spring应用程序,其中的属性文件将打包在.war文件中进行部署。如何在Spring中加载属性文件位置的系统属性

<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}" /> 

但显然春无法找到它

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

有没有人有任何想法我可以修复它? Spring是否有另一种方式访问​​系统属性?

+0

这是实际工作,这是我的错,因为我是与做错standalone.xml版本开始没有配置属性。 – aUserHimself

回答

0

它实际上是可能的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> 
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"/> 
+0

我已经知道这一点,我的问题是关于动态设置这个值,作为系统属性,然后让Spring加载它。 – aUserHimself

+1

请参阅以下... http://stackoverflow.com/questions/30640453/loading-property-file-from-system-properties-with-default-path-in-spring-context – KayV

相关问题