多个Spring配置文件的属性解析(yaml配置) [英] Property resolving for multiple Spring profiles (yaml configuration)
问题描述
将多个配置文件用于属性解析时,是否有定义的顺序.
Is there a defined order when multiple profiles are used for a property resolution.
我有yaml配置文件:
I have yaml configuration file:
name: none
---
spring:
profiles: prod
name: prodName
---
spring:
profiles: dev
name: devName
当应用程序在没有(默认)配置文件的情况下运行时,将显示 none
.对于dev/prod概要文件,已打印devName/prodName(到目前为止非常好).
When application runs with no (default) profile, none
is printed. For dev/prod profiles devName/prodName is printed (so far so good).
当我尝试将配置文件定义为 dev,prod
prodName
时,当我指定 prod,dev
devName
.
When I tried to define profile as dev,prod
prodName
is printed,when I specify prod,dev
devName
is printed.
这是我可以依靠的东西吗?我的意思是它在Spring中指定了吗?我没有找到它
Is this something I can rely on? I mean is it specified in Spring? I didn't find it here.
name: none
---
spring:
profiles: dev
name: devName
---
spring:
profiles: prod
name: prodName
Configuration.java
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
@Component
@ConfigurationProperties
public class Configuration {
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
SpringBootConsoleApplication.java
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.util.List;
@SpringBootApplication
public class SpringBootConsoleApplication implements CommandLineRunner {
private static Logger LOG = LoggerFactory.getLogger(SpringBootConsoleApplication.class);
@Autowired
Configuration conf;
public static void main(String[] args) {
SpringApplication.run(SpringBootConsoleApplication.class, args);
}
@Override
public void run(String... args) {
LOG.info("name: {}", conf.name);
}
}
示例输出:
c:\betlista\SpringPropertyResolutionMultipleProfiles>java -jar target\spring-boot-console-app-1.0.jar
...: name: none, label: labelValue
c:\betlista\SpringPropertyResolutionMultipleProfiles>java -jar -Dspring.profiles.active=dev target\spring-boot-console-app-1.0.jar
...: name: devName, label: labelValue
c:\betlista\SpringPropertyResolutionMultipleProfiles>java -jar -Dspring.profiles.active=dev,prod target\spring-boot-console-app-1.0.jar
...: name: prodName, label: labelValue
c:\betlista\SpringPropertyResolutionMultipleProfiles>java -jar -Dspring.profiles.active=prod,dev target\spring-boot-console-app-1.0.jar
...: name: devName, label: labelValue
修改2:
我将这个问题称为主题-单个弹簧轮廓的多个属性文件
There was a question which I'd refer to as topics - Multiple properties file for a single spring profile
虽然可以将 @PropertySource
与属性文件一起使用,但不能与YAML文件一起使用.我目前知道的唯一解决方案是使用例如 -Dspring.config.additional-location = classpath:topic1.yml
While one can use @PropertySource
with property files, it cannot be used with YAML files. The only solution I know at the moment is to used e.g. -Dspring.config.additional-location=classpath:topic1.yml
修改3:
重复";问题与我在这里有相同的问题,但没有答案.这个问题是关于豆子的,不是真正的财产.接受的答案是" spring.profiles.active系统属性
没关系.",我显示为不正确(在属性上下文内).如果您同意,请对重新开放进行投票.
The "duplicate" question has same questions as I have here, but without an answer. That question was about beans, not really a properties. Accepted answer says "spring.profiles.active system property
doesn't matter.", which I shown as incorrect (within context of properties). Please vote for reopen if you agree.
推荐答案
这是我可以依靠的东西吗?
Is this something I can rely on?
Yes, the last-win strategy is consistent and is documented in the reference guide:
如果指定了多个配置文件,则采用后赢策略.例如,如果配置文件prod,live由spring.profiles.active属性指定,那么application-prod.properties中的值可以被application-live.properties中的值覆盖.
If several profiles are specified, a last-wins strategy applies. For example, if profiles prod,live are specified by the spring.profiles.active property, values in application-prod.properties can be overridden by those in application-live.properties.
(不相关,但是关于您共享的文档的链接.我不知道这是否是偶然的,但这是6年前发布的Spring Boot 1.2.x的文档.)
(Unrelated, but about the link to the doc you've shared. I don't know if that's an accident but that's the documentation for Spring Boot 1.2.x which was released over 6 years ago).
这篇关于多个Spring配置文件的属性解析(yaml配置)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!