【发布时间】:2014-08-26 19:13:54
【问题描述】:
我正在尝试在基于环境(local、dev、ref、qa、prod)扩展 PropertyPlaceholderConfigurer 的类中设置属性文件
我的文件夹结构如下所示。
properties
environment.properties
server-local.properties
server-ref.properties
server-prod.properties
email-local.properties
email-ref.properties
email-prod.properties
cache-local.properties
cache-ref.properties
cache-prod.properties
environment.properties 有一个属性
environment.stage=local (or whatever env this is)
我的 Spring Integration 上下文语句如下所示:
<context:property-placeholder location="classpath:properties/*.properties" />
<bean id="propertyPlaceholder" class="com.turner.bvi.BviPropertiesUtil">
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<property name="locations">
<list>
<value>classpath:properties/environment.properties</value>
<value>classpath:properties/*-${environment.stage}.properties</value>
</list>
</property>
</bean>
我想要做的是只加载来自特定环境阶段的属性文件(无论是本地、参考、产品 .... 等)。如何根据 environment.stage 仅加载第二组属性文件?
提前感谢您的帮助。
【问题讨论】:
-
这是一个网络应用程序吗?如果您使用的是 Tomcat 之类的应用服务器,您可以将 environment.stage 设置为环境变量并读取该值以确定要选择的属性文件。以下链接应该有助于向您展示使用 Spring 读取环境变量的方法:how to read System environment variable in Spring applicationContext
-
不,这不是 Web 应用程序。我正在尝试获取与特定环境相关的那些属性文件。获取“environment.stage”变量不是问题。效果很好。获取名称中包含该 environment.stage 的文件是问题所在。 :( 我正在尝试使用“*-${environment.stage}”语句来尝试这个。我完全走错了路吗?Spring 无法解析该路径。也许在
标记中使用它不起作用。 -
在阅读了您发布的文章后,我想我可以在上下文中创建一个 util 属性标签,然后使用“ref”作为对该 id 的引用....打算试试现在。
标签: java spring properties-file spring-el