在运行时覆盖 Spring WebApp 中的属性文件
Override Properties file in Spring WebApp at Runtime
我正在使用 PropertyPlaceholderConfigurer
在我的 Spring WebApplication
中加载属性文件,如下所示:
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:db.properties</value>
<value>classpath:mail.properties</value>
</list>
</property>
</bean>
现在,我想覆盖 mail.properties
中的一些属性,因此我在我的 application-context
文件中创建了一个附加条目来读取这个 post,如下所示:
<context:property-placeholder location="file:override.properties"
order="-1" ignore-unresolvable="true" ignore-resource-not-found="true" />
然后,在我的 Tomcat Server
启动配置的 VM Arguments
中,我提供了额外的条目:
-Dexternal.props="/Users/ArpitAggarwal/Desktop/override.properties"
我必须覆盖某些键的覆盖值。
但是,WebApp 没有从 override.properties
中获取 值。谁能帮我弄清楚我错过了什么。
如有任何帮助,我们将不胜感激。谢谢!
我认为使用多个 属性Placeholders 不是一个好主意。
当两个 xml 配置在同一上下文中加载每个属性并尝试交叉使用它们时,我遇到了很多问题。
如果你想外部化一个属性文件,我建议使用 JNDI 属性:
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:db.properties</value>
<value>classpath:mail.properties</value>
<value>${java:com/env/myApp/externalProperties}
</list>
</property>
</bean>
此 JNDI 的值为:"file:/path-to-propertiesFile"。
这样你只需要一个属性ePlaceholder。
另请注意,通过将外部文件作为最后一个位置,如果您有重复的密钥,spring 将仅保留最后一个。
您只需在您的应用程序上下文中使用
<context:property-placeholder location="file:///${external.props}"
order="-1" ignore-unresolvable="true" ignore-resource-not-found="true" />
问题是你错误地使用了位置,实际位置是 vm arg 变量键 => ${external.props}
我解决问题的方法是,将location
属性替换为${ext.properties.dir:classpath:}/override.properties
,如下:
<context:property-placeholder
location="${ext.properties.dir:classpath:}/override.properties" order="-1"
ignore-unresolvable="true" ignore-resource-not-found="true" />
并从 application-server/jvm
提供 ext.properties.dir
值作为:
-Dext.properties.dir=file:/Users/ArpitAggarwal/properties/
我正在使用 PropertyPlaceholderConfigurer
在我的 Spring WebApplication
中加载属性文件,如下所示:
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:db.properties</value>
<value>classpath:mail.properties</value>
</list>
</property>
</bean>
现在,我想覆盖 mail.properties
中的一些属性,因此我在我的 application-context
文件中创建了一个附加条目来读取这个 post,如下所示:
<context:property-placeholder location="file:override.properties"
order="-1" ignore-unresolvable="true" ignore-resource-not-found="true" />
然后,在我的 Tomcat Server
启动配置的 VM Arguments
中,我提供了额外的条目:
-Dexternal.props="/Users/ArpitAggarwal/Desktop/override.properties"
我必须覆盖某些键的覆盖值。
但是,WebApp 没有从 override.properties
中获取 值。谁能帮我弄清楚我错过了什么。
如有任何帮助,我们将不胜感激。谢谢!
我认为使用多个 属性Placeholders 不是一个好主意。 当两个 xml 配置在同一上下文中加载每个属性并尝试交叉使用它们时,我遇到了很多问题。
如果你想外部化一个属性文件,我建议使用 JNDI 属性:
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:db.properties</value>
<value>classpath:mail.properties</value>
<value>${java:com/env/myApp/externalProperties}
</list>
</property>
</bean>
此 JNDI 的值为:"file:/path-to-propertiesFile"。
这样你只需要一个属性ePlaceholder。 另请注意,通过将外部文件作为最后一个位置,如果您有重复的密钥,spring 将仅保留最后一个。
您只需在您的应用程序上下文中使用
<context:property-placeholder location="file:///${external.props}"
order="-1" ignore-unresolvable="true" ignore-resource-not-found="true" />
问题是你错误地使用了位置,实际位置是 vm arg 变量键 => ${external.props}
我解决问题的方法是,将location
属性替换为${ext.properties.dir:classpath:}/override.properties
,如下:
<context:property-placeholder
location="${ext.properties.dir:classpath:}/override.properties" order="-1"
ignore-unresolvable="true" ignore-resource-not-found="true" />
并从 application-server/jvm
提供 ext.properties.dir
值作为:
-Dext.properties.dir=file:/Users/ArpitAggarwal/properties/