0%

springboot多环境配置

springboot多环境配置

springboot对于多环境配置支持多种方式

方式一:多个配置文件

在配置多个环境的配置文件时文件名可以是application-{profile}.properties/yml

默认使用application.properties/yml的配置,然后在默认配置文件中进行环境激活

1
2
3
spring:
profiles:
active: dev

方式二:yml支持多文档块

在yml配置文件中可以使用—来进行环境配置分隔,然后在每个文档块来声明环境

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
server:
port: 8081
custom:
name: 张三
spring:
profiles:
active: dev

---
spring:
profiles: dev
custom:
name: 赵柳
---
spring:
profiles: prod
custom:
name: 孙鸥

不同环境加载不同的bean

可以使用@Profile注解,限制加载bean的时机

1
2
3
4
5
6
@Bean
@Profile("dev")
public CodeReview codeReview() {

return new CodeReview();
}

欢迎关注我的其它发布渠道