首页 > 解决方案 > 从配置文件的活动列表中动态加载属性值并在配置 XML 文件中设置值

问题描述

我有一个包含批处理配置的弹簧浴应用程序。我想根据活动配置文件列表的环境变量参数动态加载配置文件。一旦我得到值,我必须在批处理配置 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>

我怎么能做到这一点?

标签: javaspringspring-batch

解决方案


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

您可以为此使用 SpEL 表达式,例如:

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

请注意,该属性已命名spring.profiles.active,而不是spring.active.profile.


推荐阅读