1、application.properties配置多环境
application.properties
#SpringBoot多环境配置,可以选择激活哪一个配置文件
spring.profiles.active=dev
application-dev.properties
server.port=8081
application-test.properties
server.port=8082
2、application.yml配置多环境
在Spring Boot中多环境配置文件名需要满足application-{profile}.yml的格式,其中{profile}对应的环境标识;
-
application-dev.yml开发环境 -
application-test.yml测试环境 -
application-prod.yml生产环境
如果我们要激活某一个环境,只需要在 application.yml里:
spring:
profiles:
active: dev
假设配置一些基本设置如:
application-dev.yml 开发环境
server:
port: 8080
application-test.yml 测试环境
server:
port: 8081
application-prod.yml 生产环境
server:
port: 8082
此时,当我们去修改application.yml:
spring:
profiles:
active: test
此时就是8081测试环境运行程序
这些也可以写在一个yaml文件中,如下:
server:
port: 8080
spring:
profiles:
active: dev
---
server:
port: 8081
spring:
config:
activate:
on-profile: dev
---
server:
port: 8082
spring:
config:
activate:
on-profile: prod
---
server:
port: 8083
spring:
config:
activate:
on-profile: test