具有不同名称的多个Spring配置文件
我有一个Spring启动应用程序,属性为application.yml。我在同一个文件中有多个配置文件,如下所示:具有不同名称的多个Spring配置文件,spring,spring-boot,Spring,Spring Boot,我有一个Spring启动应用程序,属性为application.yml。我在同一个文件中有多个配置文件,如下所示: spring: profiles: dev property: one: bla two: blabla --- spring: profiles: preProd, prod another-property: fist: bla secong: blabla --- spring: profiles: prod property: o
spring:
profiles: dev
property:
one: bla
two: blabla
---
spring:
profiles: preProd, prod
another-property:
fist: bla
secong: blabla
---
spring:
profiles: prod
property:
one: prod-bla
two: prod-blabla
因此,我的问题是,当我使用
prod
profile运行application时,Spring是否会合并这两个配置文件,并且我可以在应用程序中同时看到属性和另一个属性?合并工作非常完美
鉴于:
@SpringBootApplication
public class SoYamlSpringProfileMergeApplication {
private final Data data;
public SoYamlSpringProfileMergeApplication(Data data) {
this.data = data;
}
@EventListener(ApplicationReadyEvent.class)
public void showData() {
System.err.println(data.getOne());
System.err.println(data.getTwo());
System.err.println(data.getThree());
}
public static void main(String[] args) {
SpringApplication.run(SoYamlSpringProfileMergeApplication.class, args);
}
}
@Component
@ConfigurationProperties(prefix = "data")
class Data {
private String one = "one default";
private String two = "two default";
private String three = "three default";
public String getOne() {
return one;
}
public String getTwo() {
return two;
}
public String getThree() {
return three;
}
public void setOne(String one) {
this.one = one;
}
public void setTwo(String two) {
this.two = two;
}
public void setThree(String three) {
this.three = three;
}
}
及
将打印:
one dev
two dev
three other
以及:
spring:
profiles:
active: "other,prod"
重要活动的顺序:“其他,生产”很重要
使用
spring:
profiles:
active: "prod,other"
将输出
one dev
two dev
three other
- 从“prod”加载道具
- 与道具合并“其他”合并开发人员值
合并工作完美
鉴于:
@SpringBootApplication
public class SoYamlSpringProfileMergeApplication {
private final Data data;
public SoYamlSpringProfileMergeApplication(Data data) {
this.data = data;
}
@EventListener(ApplicationReadyEvent.class)
public void showData() {
System.err.println(data.getOne());
System.err.println(data.getTwo());
System.err.println(data.getThree());
}
public static void main(String[] args) {
SpringApplication.run(SoYamlSpringProfileMergeApplication.class, args);
}
}
@Component
@ConfigurationProperties(prefix = "data")
class Data {
private String one = "one default";
private String two = "two default";
private String three = "three default";
public String getOne() {
return one;
}
public String getTwo() {
return two;
}
public String getThree() {
return three;
}
public void setOne(String one) {
this.one = one;
}
public void setTwo(String two) {
this.two = two;
}
public void setThree(String three) {
this.three = three;
}
}
及
将打印:
one dev
two dev
three other
以及:
spring:
profiles:
active: "other,prod"
重要活动的顺序:“其他,生产”很重要
使用
spring:
profiles:
active: "prod,other"
将输出
one dev
two dev
three other
- 从“prod”加载道具
- 与道具合并“其他”合并开发人员值
为什么不能为每个环境单独设置yml文件(如application-dev.yml、application-qa.yml.application-prepod.yml、application-prod.yml)?这样,您的代码可以得到很好的维护,并且不容易出错。为什么不能在每个环境中单独设置yml文件(如application-dev.yml、application-qa.yml、application-prepod.yml、application-prod.yml)?这样,您的代码可以得到很好的维护,并且不容易出错。