簡體   English   中英

從配置文件的活動列表中動態加載屬性值並在配置 XML 文件中設置值

[英]Dynamically loading property value from active list of profiles and set the value in configuration XML file

我有一個包含批處理配置的 spring 浴缸應用程序。 我想根據活動配置文件列表的環境變量參數動態加載配置文件。一旦我得到值,我必須在批量配置 xml 文件中設置變量。 它工作正常,而活動配置文件值是單一的,如果我喜歡它,它工作正常

<context:property-placeholder location="classpath:application-${spring.active.profile}.properties" /> 

但我有一個活動配置文件列表,例如:-Dspring.active.profile=dev,localproperties,mycerts。 我想獲取活動配置文件的第一個值並加載屬性文件並加載 application-dev.properties。 我嘗試了以下方式,但將整個字符串作為配置文件值。 我如何加載活動配置文件的第一個值/。

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <util:list>
                <value>classpath:application-${spring.profiles.active}.properties</value>
            </util:list>
        </property>
        <property name="ignoreUnresolvablePlaceholders" value="true"/>
    </bean>

我怎么能做到這一點?

我有一個活動配置文件列表,例如:-Dspring.active.profile=dev,localproperties,mycerts。 我想獲取活動配置文件的第一個值

您可以為此使用 SpEL 表達式,例如:

<context:property-placeholder 
 location="classpath:application-#{environment.getActiveProfiles()[0]}.properties" />

請注意,該屬性名為spring.profiles.active而不是spring.active.profile

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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