1. 配置文件格式
1.1 以修改服务器端口为例
SpringBoot提供了多种属性配置方式
- application.properties
server.port=80
- application.yml
server:
port: 81
- application.yaml
server:
port: 82
1.2 自动提示功能消失解决方案


1.3 SpringBoot配置文件加载顺序
- application.properties > application.yml > application.yaml
2. yaml
2.1 yaml语法规则
- 大小写敏感
- 属性层级关系使用多行描述,每行结尾使用冒号结束
- 使用缩进表示层级关系,同层级左侧对齐,只允许使用空格(不允许使用Tab键)
- 属性值前面添加空格(属性名与属性值之间使用冒号+空格作为分隔)
- #表示注释
- 核心规则:数据前面要加空格与冒号隔开
2.2 yaml数组数据
- 数组数据在数据书写位置的下方使用减号作为数据开始符号,每行书写一个数据,减号与数据间空格分隔
name: 坤坤
idol:
name: 坤坤
age: 18
specialities:
- 唱
- 跳
- rap
2.3 在java程序中读取yaml中的数据
2.3.1 使用@Value读取单个数据,属性名引用方式:${一级属性名.二级属性名……}
@Value("${name}")
private String name;
@Value("${idol.specialities[0]}")
private String specialities;
2.4 封装全部数据到Environment对象
2.5 自定义对象封装指定数据
2.5.1 yml中的数据
idol:
name: 坤坤
age: 18
specialities:
- 唱
- 跳
- rap
2.5.2 定义成bean,使用@ConfigurationProperties指定前缀
@Data
@Component
@ConfigurationProperties(prefix = "idol")
public class Idol {
private String name;
private int age;
private String[] specialities;
}
2.5.3 在需要的地方直接注入使用
@Autowired
private Idol idol;
@Value("${name}")
private String name;
@Value("${idol.specialities[0]}")
private String specialities;
@GetMapping("/{id}")
public String getById(@PathVariable Integer id) {
System.out.println("id ==> " + id);
System.out.println("姓名:" + name);
System.out.println("特长:"+ specialities);
System.out.println("练习时常俩年半的idol:"+ idol);
return "hello , spring boot! ";
}
2.5.4 浏览器访问web工程,后台打印结果
3. 多环境开发配置
3.1 yaml文件多环境启动
#启动指定环境
spring:
profiles:
active: test
---
#生产环境
spring:
profiles: pro
server:
port: 8080
---
#开发环境
spring:
profiles: dev
server:
port: 8081
---
#测试环境
spring:
profiles: test
server:
port: 8083
比如说现在启动指定了test环境,看看tomcat启动显示的端口
3.2 properties文件多环境启动
主启动配置文件 application.properties
spring.profiles.active=pro
环境分类配置文件 application-pro.properties
server.port=80
环境分类配置文件 application-dev.properties
server.port=81
环境分类配置文件 application-test.properties
server.port=82
4. 多环境启动命令格式
- 带参数启动SpringBoot
java –jar springboot.jar --spring.profiles.active=test
java –jar springboot.jar --server.port=88
java –jar springboot.jar --server.port=88 --spring.profiles.active=test
5. 多环境开发控制
Maven与SpringBoot多环境兼容(步骤)
①:Maven中设置多环境属性
<profiles>
<profile>
<id>dev_env</id>
<properties>
<profile.active>dev</profile.active>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>pro_env</id>
<properties>
<profile.active>pro</profile.active>
</properties>
</profile>
<profile>
<id>test_env</id>
<properties>
<profile.active>test</profile.active>
</properties>
</profile>
</profiles>
②:SpringBoot中引用Maven属性
③:执行Maven打包指令
-
Maven指令执行完毕后,生成了对应的包,其中类参与编译,但是配置文件并没有编译,而是复制到包中
解决思路:对于源码中非java类的操作要求加载Maven对应的属性,解析${}占位符
④:对资源文件开启对默认占位符的解析
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>utf-8</encoding>
<useDefaultDelimiters>true</useDefaultDelimiters>
</configuration>
</plugin>
</plugins>
</build>
- Maven打包加载到属性,打包顺利通过
6. 配置文件分类
-
SpringBoot中4级配置文件
1级: file :config/application.yml 【最高】
2级: file :application.yml
3级:classpath:config/application.yml
4级:classpath:application.yml 【最低】
-
作用:
1级与2级留做系统打包后设置通用属性
3级与4级用于系统开发阶段设置通用属性