The
spring.profiles.active
property(属性) follows the same ordering rules as other properties: The highestPropertySource
wins. This means that you can specify(明确指定) active profiles inapplication.properties
and then replace them by using the command line switch.Sometimes, it is useful to have profile-specific properties that add to the active profiles rather than replace them. The
spring.profiles.include
property can be used to unconditionally(无条件地) add active profiles. TheSpringApplication
entry point also has a Java API for setting additional profiles (that is, on top of those activated by thespring.profiles.active
property). See thesetAdditionalProfiles()
method in SpringApplication.For example, when an application with the following properties is run by using the switch(转换),
--spring.profiles.active=prod
, theproddb
andprodmq
profiles are also activated:---
my.property: fromyamlfile
---
spring.profiles:
prod spring.profiles.include:
- proddb
- prodmq
不同的环境下,会有不同的配置。
applcation.properties下可以使用下面的激活环境配置:
spring.profiles.active = dev
一般默认,dev为开发环境、test为测试环境、prod为生产环境。可以将对应的配置文件命名为application-dev.properties、application-prod.properties、application-test.properties。
profile.name=dev
profile.name=test
profile.name=prod
applcation.yml可以使用下面的激活环境配置。applcation.yml支持以“---”分割的多文档块方式。
spring:
profiles:
active: dev
---
spring:
profiles:
active: test
---
spring:
profiles:
active: prod
配置文件之间存在互补配置。
加载顺序可以根据配置文件存放的路径来看,从上到下,优先级从高到低。前两者在程序包下,后两者在java文件夹下:
- file:./config/
- file:./
- classpath:./config/
- classpath:./
外部配置加载的顺序,首先是命令行参数,然后由jar包外向内,优先加载带spring.profiles的配置文件。
profile参考文档地址:https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-profiles