从配置文件的活动列表动态加载 属性 值并在配置 XML 文件中设置值

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

我有一个 spring 包含批处理配置的沐浴应用程序。我想根据活动配置文件 list.once 的环境变量参数动态加载配置文件我得到了值,我必须在批处理配置 xml 文件中设置变量。它工作正常,而活动配置文件值是单一的,如果我给它,它工作正常

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

但我有一个活动配置文件列表 例如:-Dspring.active.profile=dev、localproperties、mycerts。我想获取活动配置文件的第一个值并加载属性文件并加载应用程序-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>

我怎样才能做到这一点?

i have a list of active profiles eg: -Dspring.active.profile=dev,localproperties,mycerts. I want to take first value of active profile

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

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

请注意 属性 被命名为 spring.profiles.active 而不是 spring.active.profile