配置文件
配置文件格式
application.properties
server.port=80
application.yml(主流)
server:
port: 81
application.yaml
server:
port: 82
配置文件不出提示怎么办:
点击idea右上角project-structure、facets、找到spring下自己的项目,选中,看到没有yaml或yml,点击上面的绿叶图标,点击+,找到resource下的相应文件,最后确定就行。
SpringBoot配置文件加载顺序:
application.properties > application.yml > application.yaml
yaml语法规则
大小写敏感
使用缩进表示层级关系,同层级左侧对齐,只允许使用空格(不允许使用Tab键)
属性值前面添加一个空格(属性名与属性值之间使用冒号+空格作为分隔)
注释为#
数组使用减号开始,每行写一个数据,减号与数据间一个空格分隔
配置实例与读取方式
lesson: SpringBoot
server:
port: 82
enterprise:
name: itcast
age: 16
tel: 4006184000
subject:
- Java
- 前端
- 大数据
@RestController
@RequestMapping("/books")
public class BookController {
//使用@Value读取单一属性数据
@Value("${lesson}")
private String lesson;
@Value("${server.port}")
private Integer port;
@Value("${enterprise.subject[0]}")
private String subject_00;//使用Environment封装全配置数据
@Autowired
private Environment environment;@Autowired
private Enterprise enterprise;
@GetMapping("/{id}")
public String getById(@PathVariable Integer id){
System.out.println(lesson);
System.out.println(port);//直接读取配置文件的形式
System.out.println(subject_00);
System.out.println("--------------------");
System.out.println(environment.getProperty("lesson"));
System.out.println(environment.getProperty("server.port"));//读取对象形式
System.out.println(environment.getProperty("enterprise.age"));
System.out.println(environment.getProperty("enterprise.subject[1]"));
System.out.println("---------------------");
System.out.println(enterprise);
return "hello , spring boot!";
}}
读取对象形式
自定义对象封装数据警告解决方案:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
//封装yaml对象格式数据必须先声明当前实体类受Spring管控
@Component
//读取配置属性信息,通过prefix属性设置读取哪个数据
@ConfigurationProperties(prefix = "enterprise")
public class Enterprise {
private String name;
private Integer age;
private String tel;
private String[] subject;...
多环境参数的配置
一个yml中配置多个环境
多个环境用---分隔
spring:
profiles:
active: dev---
#开发环境#新的指定方式
spring:
config:
activate:
on-profile: dev
server:
port: 80
---
#生产环境
spring:
profiles: pro
server:
port: 81
---
#测试环境
spring:
profiles: test
server:
port: 82
多个properties文件配合指定
主配置文件application.properties
spring.profiles.active=dev
application-pro.properties
server.port=80
application-dev.properties
server.port=81
application-test.properties
server.port=82
带启动参数启动SpringBoot
java –jar springboot.jar –-spring.profiles.active=test
java –jar springboot.jar –-server.port=88 –-spring.profiles.active=test
加载顺序参考:Core Features
maven解析作用于配置文件
由于springboot是由maven打包的,所以maven可以对springboot(这里指配置信息)进行一定的操作。
pom指定信息:
<profiles>
<!--开发环境-->
<profile>
<id>dev</id>
<properties>
<profile.active>dev</profile.active>
</properties>
</profile>
<!--生产环境-->
<profile>
<id>pro</id>
<properties>
<profile.active>pro</profile.active>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<!--测试环境-->
<profile>
<id>test</id>
<properties>
<profile.active>test</profile.active>
</properties>
</profile>
</profiles>
pom添加插件信息:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<encoding>UTF-8</encoding>
<useDefaultDelimiters>true</useDefaultDelimiters><!--开启解析功能-->
</configuration>
</plugin>
配置文件:
spring:
profiles:
active: ${profile.active}---
#开发
spring:
profiles: dev
server:
port: 80
---
#生产
spring:
profiles: pro
server:
port: 81
---
#测试
spring:
profiles: test
server:
port: 82
启动参数项过多问题的解决方案:
在jar包在同一级添加一个application.yml文件,会被自动读取,覆盖springboot本身的。
SpringBoot中4级配置文件
1级: 和jar包在同一级:config/application.yml 【最高】
2.4.6和2.5.0有个bug,需要在config下有个文件夹才行。2.4.5没有这个问题。
2级: 和jar包在同一级:application.yml
3级:classpath:config/application.yml
4级:classpath:application.yml