Spring-boot 2中的多个配置文件属性配置

时间:2019-04-01 12:53:10

标签: java spring-boot spring-profiles

我有一个带有多个properties文件的spring-boot 2.1.3.RELEASE应用程序。

在我的/src/main/resources中,我有my-app.properties并已配置my-app-local.properties

此外,在项目之外,我还有另一个配置文件的属性文件/config/my-app-local.properties

此配置的重点是具有以下属性层次结构:

  1. /resources/my-app.properties
  2. /resources/my-app-local.properties
  3. /config/my-app-local.properties

因此,当我尝试使用以下参数运行应用程序时:

--spring.profiles.active=local --spring.config.name=my-app --spring.config.location=config/my-app.properties

该应用程序无法启动,因为找不到任何properties个文件。

但是,此配置和参数在spring-boot 1.5.19.RELEASE上运行良好。在spring-boot 2中如何实现相同的目的?

1 个答案:

答案 0 :(得分:1)

使用spring.config.additional-location doc

  

使用spring.config.location配置自定义配置位置时,它们替换默认位置。例如,如果spring.config.location配置为值classpath:/custom-config/,file:./custom-config/,,则搜索顺序如下:

file:./custom-config/
classpath:custom-config/
  

或者,当使用spring.config.additional-location,配置自定义配置位置时,除默认位置外,还将使用它们。在默认位置之前搜索其他位置。例如,如果配置了classpath:/custom-config/,file:./custom-config/的其他位置,则搜索顺序将变为:

file:./custom-config/
classpath:custom-config/
file:./config/
file:./
classpath:/config/
classpath:/
相关问题