![](/img/trans.png)
[英]How to set Spring Active Profiles Pragmatically from a configuration class?
[英]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.