
1.开发环境
- JDK版本:17.0.3
- IDEA版本: 2022.1.4
- Maven版本:3.8.6
- Spring Boot版本:3.0.0
2.application.properties配置文件
2.1 创建Module模块
- 创建spring-boot-properties模块




2.2 语法规则
2.3 内置属性的使用
- 编写application.properties文件




![]()

2.4 自定义属性的使用
- 对于自定义的属性可以通过自动注入的方式达到内置的效果



- 修改application.properties文件

- 修改PropertiesController类的代码


2.5 ConfigurationProperties注解
- 若需要注入的属性过多时,则创建类和ConfigurationProperties注解进行处理。
- 创建JDBCProperties类

- 修改PropertiesController类的代码


3.application.yml配置文件
3.1 基本概念
- application.yml是一种yaml格式的配置文件,一种能够被计算机识别的数据序列化格式,更容易被人们阅读,类似于xml文件。
3.2 创建Module模块


3.3 语法规则
- 语法规则:键: 值,其中冒号和值之间必须有一个空格,使用tab键来划分层级。
- properties配置文件的优先级高于yml配置文件,若希望使用yml配置文件,则删除或者修改properties配置文件的名称。
3.4 内置属性的使用



3.5 自定义属性的使用
- 对于自定义的属性可以通过自动注入的方式达到内置的效果。



3.6 ConfigurationProperties注解的方式




4.多环境配置
4.1 展现形式
- 使用application-自定义.properties或application-自定义.yml的方式配置多种环境。
- 通常的命名方式有:
- dev表示开发环境
- test表示测试环境
- uat表示准生产环境
- pro表示生产环境
4.2 通过总配置文件切换
- 每种环境对应一个相应的配置文件,使用总配置文件进行切换。
- 编写application-dev.yml文件的代码:

- 编写application-test.yml文件的代码:

- 编写appliation-uat.yml文件的代码:

- 编写application-pro.yml文件的代码:



4.3 通过Maven配置进行切换





更多精彩欢迎关注微信公众号《格子衫007》!
来源:https://www.cnblogs.com/gezishan007/p/16990394.html
本站部分图文来源于网络,如有侵权请联系删除。