当spring.profiles.active设置了多个Spring环境配置文件时,优先顺序是什么? [英] What is the order of precedence when there are multiple Spring's environment profiles as set by spring.profiles.active

查看:3423
本文介绍了当spring.profiles.active设置了多个Spring环境配置文件时,优先顺序是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想知道当指定了多个Spring活动配置文件时,优先顺序是什么.

I am just wondering what the order of precedence is when multiple Spring active profiles have been specified.

说我希望default配置文件处于活动状态,但是dev配置文件在有多个相同元素(例如bean)可供选择但具有不同配置文件的情况下覆盖它...

Say I want the default profile to be active but the dev profile to override it when there are several identical elements (beans for instance) to choose from but with different profiles...

例如,我有两个PropertySourcesPlaceholderConfigurer bean配置了"default""dev"值以及环境配置文件.

Say for instance I have two PropertySourcesPlaceholderConfigurer beans configured with "default" and "dev" values a environment profiles.

如果我使用以下配置文件激活:-Dspring.profiles.active="default,dev"

If I use the following profile activation: -Dspring.profiles.active="default,dev"

dev配置文件是否会覆盖default一个?

Will the dev profile override the default one?

如果不能实现上述行为?

If not how can the above behavior be achieved?

推荐答案

spring.profiles.active系统属性中的配置文件顺序无关紧要. 优先级"由Bean的声明顺序定义,包括特定于配置文件的Bean,并且最后一个Bean定义获胜.

The order of the profiles in the spring.profiles.active system property doesn't matter. "Precedence" is defined by the declaration order of the beans, including beans specific to a profile, and the last bean definition wins.

以您的示例为例,如果使用-Dspring.profiles.active="default,dev",则将在default配置文件中使用props bean,仅因为它是该bean的最后一个活动定义:

Using your example, if -Dspring.profiles.active="default,dev" is used, the props bean in the default profile would be used here, simply because it's the last active definition of that bean:

<beans profile="dev">
    <bean id="props" class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
        <property name="location" value="classpath:META-INF/dev.properties"/>
    </bean>
</beans>
<beans profile="default">
    <bean id="props" class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
        <property name="location" value="classpath:META-INF/default.properties"/>
    </bean>
</beans>

反转Bean的顺序,然后使用dev版本,而不管spring.profiles.active中的概要文件如何排序.

Invert the order of the beans, and then the dev version would be used, regardless of how the profiles are ordered in spring.profiles.active.

请注意,我没有使用<context:property-placeholder/>,因为它不允许您显式指定bean id,因此,我不确定如果使用多个bean,它将显示什么行为.我想可以合并这些属性,以便由两者定义的属性都使用最后一个定义,但是特定于每个文件的属性将保持不变.

Notice that I did not use <context:property-placeholder/> because it does not allow you to explicitly specify a bean id, and so I'm not sure what behavior it would exhibit if more than one is used. I imagine that the properties would be merged, so that properties defined by both would use the last definition, but properties specific to each file would remain intact.

否则,以我的经验,您通常会按以下顺序定义bean:

Otherwise, in my experience, you would typically define beans in this order:

  1. 默认" bean定义,不是特定于概要文件的
  2. 在特定于环境的配置文件中覆盖bean定义
  3. 在测试专用配置文件中覆盖bean定义

这样,如果将测试概要文件bean与其他概要文件结合使用,则将获胜;否则,您将根据配置文件使用特定于环境的Bean或默认Bean.

This way, test profile beans would win if used in combination with other profiles; else you would either use environment-specific beans or default beans based on the profile.

这篇关于当spring.profiles.active设置了多个Spring环境配置文件时,优先顺序是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆