Spring-boot 2 中的多个配置文件属性配置
Multiple profiled properties configuration in Spring-boot 2
我有一个 spring-boot 2.1。3.RELEASE 应用程序有多个 properties
个文件。
在我的 /src/main/resources
中,我有 my-app.properties
并分析了 my-app-local.properties
。
此外,在项目之外我还有另一个配置文件 /config/my-app-local.properties
。
此配置的要点是具有以下属性层次结构:
/resources/my-app.properties
/resources/my-app-local.properties
/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 中实现同样的效果?
使用spring.config.additional-location
doc
When custom config locations are configured by using spring.config.location
, they replace the default locations. For example, if spring.config.location
is configured with the value classpath:/custom-config/,file:./custom-config/,
the search order becomes the following:
file:./custom-config/
classpath:custom-config/
Alternatively, when custom config locations are configured by using spring.config.additional-location,
they are used in addition to the default locations. Additional locations are searched before the default locations. For example, if additional locations of classpath:/custom-config/,file:./custom-config/
are configured, the search order becomes the following:
file:./custom-config/
classpath:custom-config/
file:./config/
file:./
classpath:/config/
classpath:/
我有一个 spring-boot 2.1。3.RELEASE 应用程序有多个 properties
个文件。
在我的 /src/main/resources
中,我有 my-app.properties
并分析了 my-app-local.properties
。
此外,在项目之外我还有另一个配置文件 /config/my-app-local.properties
。
此配置的要点是具有以下属性层次结构:
/resources/my-app.properties
/resources/my-app-local.properties
/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 中实现同样的效果?
使用spring.config.additional-location
doc
When custom config locations are configured by using
spring.config.location
, they replace the default locations. For example, ifspring.config.location
is configured with the valueclasspath:/custom-config/,file:./custom-config/,
the search order becomes the following:
file:./custom-config/
classpath:custom-config/
Alternatively, when custom config locations are configured by using
spring.config.additional-location,
they are used in addition to the default locations. Additional locations are searched before the default locations. For example, if additional locations ofclasspath:/custom-config/,file:./custom-config/
are configured, the search order becomes the following:
file:./custom-config/
classpath:custom-config/
file:./config/
file:./
classpath:/config/
classpath:/