簡體   English   中英

在值標簽中使用Spring Integration SpEl

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM