设置多个配置文件的弹簧配置

时间:2018-06-18 21:34:32

标签: java spring spring-boot configuration

我的应用配置文件application.yml中有多个配置文件,如下例所示:

spring:
  application:
    name: my-super-app
database:
  secret: "default secret"
this:
  that: "default value..."

---

spring:
  profiles: staging


---

spring:
  profiles: qa
database:
  secret: "foo bar"

---

spring:
  profiles: playground
database:
  secret: "foo bar"

---

spring:
  profiles: production
database:
  secret: "foo bar"

很明显,我为database.secretqaplayground配置文件冗余设置production配置,staging除外。有没有办法为这三个配置文件设置一次,对配置文件进行分组或从基本配置文件继承?

1 个答案:

答案 0 :(得分:0)

您可以将逗号分隔列表(qa,playground,production)组合在一起,如下所示:

spring:
  application:
    name: my-super-app
common-secret: "foo bar"
database:
  secret: "default secret"
this:
  that: "default value..."

---

spring:
  profiles: staging


---

spring:
  profiles: qa

---

spring:
  profiles: playground

---

spring:
  profiles: production

---

spring:
  profiles: qa,playground,production
database:
    secret: "foo bar"

或者你可以设置一个“共享变量”,如下所示:

spring:
  application:
    name: my-super-app
common-secret: "foo bar"
database:
  secret: "default secret"
this:
  that: "default value..."

---

spring:
  profiles: staging


---

spring:
  profiles: qa
database:
  secret: ${common-secret}

---

spring:
  profiles: playground
database:
  secret: ${common-secret}

---

spring:
  profiles: production
database:
    secret: ${common-secret}