我有一个Spring Boot应用程序,我有application.yml作为属性。我在同一个文件中有多个配置文件,如下所示:
private static URL getVertexConfiguration() throws MalformedURLException {
try {
// Code omitted
} catch ( Throwable th ) {
th.printStackTrace();
return null;
}
}
所以我的问题是,当我仅使用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
个配置文件 运行应用程序时,Spring会合并两个配置文件,并且我可以在应用程序中同时看到prod
和property
?
答案 0 :(得分:2)
合并效果很好!
给定:
@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;
}
}
和
spring:
profiles:
active: "other"
---
spring:
profiles: dev
data:
one: one dev
two: two dev
---
spring:
profiles: prod
data:
one: one prod
two: two prod
---
spring:
profiles: other
data:
three: three other
将打印:
one dev
two dev
three other
并带有:
spring:
profiles:
active: "other,prod"
one prod
two prod
three other
重要的有效顺序:“其他产品” 很重要!
使用
spring:
profiles:
active: "prod,other"
将输出
one dev
two dev
three other