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

jgovgodb  于 2021-07-23  发布在  Java
关注(0)|答案(1)|浏览(522)

我有一个springbath应用程序,它包含一个批处理配置。我想根据activeprofile list的environmentvariable参数动态加载profile,一旦得到值,就必须在批配置xml文件中设置变量。它的工作罚款,而活动配置文件值是单一的,它的工作罚款,如果我给喜欢

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

但是我有一个活动概要文件的列表,例如:-dspring.active.profile=dev,localproperties,mycerts。我想获取活动概要文件的第一个值,然后加载属性文件并加载application-dev.properties。我尝试了下面的方法,但是得到了整个字符串作为配置文件值。如何加载active profile/的第一个值。

<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>

我怎样才能做到这一点?

k5ifujac

k5ifujac1#

我有一个活动配置文件列表,例如:-dspring.active.profile=dev、localproperties、mycert。我要取活动配置文件的第一个值
您可以使用spel表达式,例如:

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

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

相关问题