多环境配置及配置文件位置
# SpringBoot多环境配置及配置文件位置
# 1、application.properties配置多环境
application.properties
#SpringBoot多环境配置,可以选择激活哪一个配置文件
spring.profiles.active=dev
1
2
2
application-dev.properties
server.port=8081
1
application-test.properties
server.port=8082
1
# 2、application.yml配置多环境
在Spring Boot
中多环境配置文件名需要满足application-{profile}.yml
的格式,其中{profile}
对应的环境标识;
application-dev.yml
开发环境application-test.yml
测试环境application-prod.yml
生产环境
如果我们要激活某一个环境,只需要在 application.yml
里:
spring:
profiles:
active: dev
1
2
3
2
3
假设配置一些基本设置如:
application-dev.yml
开发环境
server:
port: 8080
1
2
2
application-test.yml
测试环境
server:
port: 8081
1
2
2
application-prod.yml
生产环境
server:
port: 8082
1
2
2
此时,当我们去修改application.yml
:
spring:
profiles:
active: test
1
2
3
2
3
此时就是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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
编辑 (opens new window)