二、基础配置
1. 配置文件格式
问题导入
框架常见的配置文件有哪几种形式?
1.1 修改服务器端口
http://localhost:8080/books/1 >>> http://localhost/books/1
SpringBoot提供了多种属性配置方式
server.port=80
application.yml
server:
port: 81
application.yaml
server:
port: 82
1.2 自动提示功能消失解决方案
操作步骤:
1.3 SpringBoot配置文件加载顺序(了解)
注意事项:
-
SpringBoot核心配置文件名为application
-
SpringBoot内置属性过多,且所有属性集中在一起修改,在使用时,通过提示键+关键字修改属性
2. yaml
问题导入
什么是yaml,和properties有什么区别?
2.1 yaml语法规则
2.2 yaml数组数据
2.3 yaml数据读取
封装全部数据到Environment对象
==自定义对象封装指定数据【常用】==
public class Enterprise {
private String name;
private Integer age;
private String tel;
private String[] subject;
//自行添加getter、setter、toString()等方法
}
自定义对象封装数据警告解决方案
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
3. 多环境开发配置
问题导入
在实际开发中,项目的开发环境、测试环境、生产环境的配置信息是否会一致?如何快速切换?
3.1 多环境启动配置
3.2 多环境启动命令格式
3.3 多环境开发控制
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打包指令
④:对资源文件开启对默认占位符的解析
<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>utf-8</encoding>
<useDefaultDelimiters>true</useDefaultDelimiters>
</configuration>
</plugin>
</plugins>
</build>
Maven打包加载到属性,打包顺利通过
4. 配置文件分类
问题导入
SpringBoot的配置文件可以放在项目的哪些地方?
java –jar springboot.jar --spring.profiles.active=test --server.port=85 --server.servlet.context-path=/heima --server.tomcat.connection-timeout=-1 ... ...
-
SpringBoot中4级配置文件
1级: file :config/application.yml 【最高】
2级: file :application.yml
3级:classpath:config/application.yml
4级:classpath:application.yml 【最低】
-
作用:
1级与2级留做系统打包后设置通用属性
3级与4级用于系统开发阶段设置通用属性