Spring配置文件属性与单个属性文件的继承

时间:2019-04-17 08:01:53

标签: spring-boot yaml

我有一个application.yml文件,其中包含多个弹簧轮廓。 我想将属性从一个配置文件继承到另一个。

在此示例中,我想将prod概要文件的属性继承到prod1中,而无需再次在prod1概要文件中写入公共属性。

  server:
    port: 8080

  spring:
    datasource:
      driver-class-name: com.mysql.cj.jdbc.Driver
    application:
      name: TestApp

    URL: "https://localhost:8181/Services/IDEA-Client-Partners"
  ---
  spring:
    profiles: dev
  ---
  spring:
    profiles: prod

  URL: https://www.ideaedu.org/Services/IDEA-Client-Partners

  ---
  spring:
    profiles: prod1

1 个答案:

答案 0 :(得分:1)

如果激活了多个配置文件,则

属性确实已经继承。例如。如果您激活prodprod1,则所有默认

给出您的示例,

 server:
    port: 8080

  spring:
    datasource:
      driver-class-name: com.mysql.cj.jdbc.Driver
    application:
      name: TestApp

    URL: "https://localhost:8181/Services/IDEA-Client-Partners"
  ---
  spring:
    profiles: dev
  ---
  spring:
    profiles: prod

  URL: https://www.ideaedu.org/Services/IDEA-Client-Partners
  prodProperty: test
  ---
  spring:
    profiles: prod1

  URL: https://localhost/

并激活所有个人资料,-Dspring.profiles.active=prod,prod1

将设置以下属性,

  • 端口= 8080
  • diver-class-name = com.mysql.cj.jdbc.Driver
  • 名称= TestApp
  • prodProperty =测试
  • URL = https://localhost/

如果属性冲突,例如在此示例中,URL将赢得最后一个属性读取,即,当prodprod1处于活动状态时,将读取最后一个属性,在这种情况下,将使用prod1的定义。