【问题标题】:Using Spring Integration SpEl inside value tag在值标签内使用 Spring Integration SpEl
【发布时间】: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


【解决方案1】:

您可以为此使用 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

您也可以使用 -Dspring.profiles.active=local 等 JVM 参数

如果您需要按原样维护环境变量,您可以实现自定义 ActiveProfilesResolver,如下所述:Spring Profiles: Simple example of ActiveProfilesResolver?

【讨论】:

  • 非常感谢。这是一个很好的解决方案,但我也能够使用通过 Chef 设置的环境变量或通过创建一个包含环境变量的 Docker 容器来实现该目的。请参阅下面的帖子。
【解决方案2】:

感谢大家的帮助。我能够采纳您的所有建议,并在 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>

再次感谢大家的帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多