spring.profiles.active设置的多个Spring环境配置文件的优先顺序是什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23617831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the order of precedence when there are multiple Spring's environment profiles as set by spring.profiles.active
提问by balteo
I am just wondering what the order of precedence is when multiple Spring active profiles have been specified.
我只是想知道指定多个 Spring 活动配置文件时的优先顺序是什么。
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...
假设我希望default
配置文件处于活动状态,但是dev
当有几个相同的元素(例如 bean)可供选择但具有不同的配置文件时,配置文件将覆盖它...
Say for instance I have two PropertySourcesPlaceholderConfigurer
beans configured with "default"
and "dev"
values a environment profiles.
举例来说,我有两个PropertySourcesPlaceholderConfigurer
bean 配置了一个环境配置文件"default"
并为其"dev"
赋值。
If I use the following profile activation: -Dspring.profiles.active="default,dev"
如果我使用以下配置文件激活: -Dspring.profiles.active="default,dev"
Will the dev
profile override the default
one?
该dev
配置文件会覆盖该配置文件default
吗?
If not how can the above behavior be achieved?
如果不是,如何实现上述行为?
采纳答案by superEb
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.
spring.profiles.active
系统属性中配置文件的顺序无关紧要。“优先级”由 bean 的声明顺序定义,包括特定于配置文件的 bean,最后一个 bean 定义 wins。
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:
使用您的示例,如果-Dspring.profiles.active="default,dev"
使用,则将props
在default
此处使用配置文件中的bean ,因为它是该 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>
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
.
反转 bean 的顺序,然后dev
将使用版本,无论配置文件在spring.profiles.active
.
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.
请注意,我没有使用,<context:property-placeholder/>
因为它不允许您显式指定 bean id,因此我不确定如果使用多个 bean 会表现出什么行为。我想这些属性将被合并,这样两者定义的属性将使用最后一个定义,但特定于每个文件的属性将保持不变。
Otherwise, in my experience, you would typically define beans in this order:
否则,根据我的经验,您通常会按以下顺序定义 bean:
- "Default" bean definitions, not specific to a profile
- Overriding bean definitions in an environment-specific profile
- Overriding bean definitions in a test-specific profile
- “默认”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.
这样,如果与其他配置文件结合使用,测试配置文件 bean 将获胜;否则,您将根据配置文件使用特定于环境的 bean 或默认 bean。
回答by Patrick Cornelissen
superEB is right the order of the profiles doesn't matter for beans, the declaration order is more important there, but keep in mind that the order is important if you use profile based configuration files!
superEB 是正确的,配置文件的顺序对 bean 无关紧要,声明顺序在那里更重要,但请记住,如果您使用基于配置文件的配置文件,顺序很重要!