Spring Pofiles多环境开发

简介

SpringBoot支持多环境开发,通过配置文件来控制环境,默认是dev环境,可以通过命令行参数来指定环境,如:java -jar xxx.jar --spring.profiles.active=test

分割不同环境的配置

使用

1
---

指定配置属于哪个环境

通过键值来指定,如:spring.config.activate.on-profile=<环境配置名称>

如何指定那个环境的配置生效?

1
2
3
spring:
profiles:
active: <环境配置名称>a

例子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
spring:
profiles:
active: dev #指定默认环境

---
#开发环境
spring:
congfig:
activate:
on-profile: dev
---
#测试环境
spring:
congfig:
activate:
on-profile: test
---
#生成环境
spring:
congfig:
activate:
on-profile: pro

配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
spring:
profiles:
active: home
# active: school
application:
name: big-event
servlet:
multipart:
max-request-size: 100MB
max-file-size: 10MB
data:
redis:
host: localhost
port: 6379
# mybatis自动大小写
mybatis:
configuration:
map-underscore-to-camel-case: true
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
1
2
3
4
5
6
7
8
9
spring:
config:
profiles:
on-profile: home
datasource:
url: jdbc:h2:tcp://localhost/C:/Users/leegw/Documents/java/big-enent/database/database
username: sa
password: sa
driverClassName: org.h2.Driver
1
2
3
4
5
6
7
8
9
10
spring:
config:
profiles:
on-profile: school
datasource:
url: jdbc:h2:tcp://localhost/E:/java/big-event/database/database
username: sa
password: sa
driverClassName: org.h2.Driver