Java 使用Spring集成SpEl内部值标记

Java 使用Spring集成SpEl内部值标记,java,spring,properties-file,spring-el,Java,Spring,Properties File,Spring El,我试图在一个类中设置属性文件,该类基于本地环境、dev、ref、qa、prod扩展PropertyPlaceHolderConfigure 我的文件夹结构如下所示 properties environment.properties server-local.properties server-ref.properties server-prod.properties email-local.properties email-ref.properties

我试图在一个类中设置属性文件,该类基于本地环境、dev、ref、qa、prod扩展PropertyPlaceHolderConfigure

我的文件夹结构如下所示

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集成上下文语句如下所示:

<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>
<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>
...
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
我想做的是,只从特定环境阶段加载属性文件,可以是local、ref、prod。。。。等如何根据environment.stage加载第二组属性文件


提前感谢您的帮助。

您可以使用Spring配置文件进行此操作,它将类似于:

<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>
<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>
...
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
可以使用spring\u profiles\u active或spring\u profiles\u default设置环境变量。在Unix中,尝试导出SPRING\u配置文件\u DEFAULT=local

您可以交替使用JVM参数,如-Dspring.profiles.active=local


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

谢谢大家的帮助。我能够从您的建议中提取一些片段,并在Spring上下文中使用environmentProperties提出一个解决方案

尝试在上下文中使用的问题是,在我的类尝试解析${environment.stage}时,它还没有被设置。。。或者至少这是我从搜索其他帖子中得到的信息

如果我的属性文件结构如下所示:

<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>
<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>
...
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容器将环境属性“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>

再次感谢大家的帮助。

这是一个web应用程序吗?如果您使用的是像Tomcat这样的应用程序服务器,则可以将environment.stage设置为环境变量,并读取该值以确定要选择的属性文件。下面的链接应该有助于向您展示使用Spring读取环境变量的方法:不,这不是web应用程序。我正在尝试获取与特定环境相关的属性文件。获取environment.stage变量不是问题所在。这很有效。获取名称中包含environment.stage的文件是一个问题:我正在尝试使用*-${environment.stage}语句来尝试此操作。我完全走错路了吗?Spring无法解析该路径。也许在标记中使用它不起作用。在阅读了您发布的文章之后,我想我可能能够在上下文中创建一个util属性标记,然后使用ref作为对该id的引用。。。。我现在就试试。非常感谢。这是一个很好的解决方案,但我也能够通过使用通过Chef设置的环境变量或通过创建包含环境变量的Docker容器来实现这一目的。见下面的帖子。