application.properties 是否可以依赖于多个配置文件?

Enl*_*tMe 5 profile spring-boot

Spring Boot 属性文件是否可以依赖于两个或多个配置文件?就像是application-profile1-profile2.properties

kya*_*kya 1

我知道的有4种方法。

  1. 插入.yaml.properties以编程方式像 Asi Bross Said 一样。使用ResourceLoaderYamlPropertySourceLoader来插入。

  2. 使用.yaml。但当你有另一个 spring 项目依赖它时,它将被替换。

  3. 使用属性而不是配置文件。(对于api项目)

    1. 使用 1@PropertySource来定义属性文件 A。
    2. 从属性文件 A 中获取变量并将它们分配给另一个文件路径表达式中的参数@PropertySource

    例如:

    resources
    /-application.properties  <-- remove or empty,because it will be override by application project
    /-moduleA
         /-application.properties               <-- Intellij can identify properties files started with application-
         /-application-mysql-dev.properties
         /-application-psql-dev.properties
         /-application-psql-prod.properties
    
    Run Code Online (Sandbox Code Playgroud)

    内容resources/moduleA/application.properties

    moduleA.custom.profile1=mysql
    moduleA.custom.profile2=dev
    
    Run Code Online (Sandbox Code Playgroud)

    Java配置文件的内容:

    resources
    /-application.properties  <-- remove or empty,because it will be override by application project
    /-moduleA
         /-application.properties               <-- Intellij can identify properties files started with application-
         /-application-mysql-dev.properties
         /-application-psql-dev.properties
         /-application-psql-prod.properties
    
    Run Code Online (Sandbox Code Playgroud)
  4. 使用属性而不是配置文件。(针对申请项目)

    resources
    /-application.properties
    /-application-mysql-dev.properties
    /-application-psql-dev.properties
    /-application-psql-prod.properties
    
    Run Code Online (Sandbox Code Playgroud)

    内容resources/application.properties

    moduleA.custom.profile1=mysql
    moduleA.custom.profile2=dev
    
    Run Code Online (Sandbox Code Playgroud)

    内容SpringMvcApplication.java

    moduleA.custom.profile1=mysql
    moduleA.custom.profile2=dev
    
    Run Code Online (Sandbox Code Playgroud)