[英]Using Spring Integration SpEl inside value tag
我试图在基于环境(本地,开发,引用,qa,产品)扩展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>
我想要做的是仅具有来自特定环境阶段负载的属性文件(无论是本地,ref,prod ...等)。 如何仅根据环境加载第二组属性文件?
我在这里先向您的帮助表示感谢。
您可以为此使用Spring配置文件,就像这样:
<context:property-placeholder location="classpath:properties/*.properties" />
<beans profile="local">
<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/*-local.properties</value>
</list>
</property>
</bean>
</beans>
<beans profile="dev">
<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/*-local.properties</value>
</list>
</property>
</bean>
</beans>
...
可以使用spring_profiles_active(或spring_profiles_default)来设置环境变量。 在Unix中,尝试导出SPRING_PROFILES_DEFAULT = local
您也可以使用JVM参数,例如-Dspring.profiles.active = local
如果需要按原样维护环境变量,则可以按如下所述实现自定义ActiveProfilesResolver: Spring Profiles:ActiveProfilesResolver的简单示例?
谢谢大家的帮助。 我能够摘录您所有的建议,并在Spring上下文中使用“ environmentProperties”提出了一个解决方案。
尝试在上下文中使用a的问题是,在我的班级尝试解析$ {environment.stage}时,尚未设置它。或者至少这是我从搜索其他帖子中收集的。
如果我的属性文件结构如下所示:
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
我可以通过Chef或通过Docker容器将Environment属性'env'设置为正确的值,并使用以下代码。
<!-- Register the properties file location -->
<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/*-#{systemEnvironment['env']}.properties</value>
</list>
</property>
</bean>
我可以将每个属性集放在其自己的目录下,并具有
<value>classpath:properties/#{systemEnvironment['env']}/*.properties</value>
再次感谢大家的帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.