一、配置文件格式

SpringBoot提供了多种属性配置方式

  1. server.port=80
  1. server:
  2. port: 81
  1. server:
  2. port: 82

1、环境准备

image.png


2、三种配置文件优先级

properties > yml > yaml

SpringBoot 中4级配置文件放置位置:
1级:classpath:application.yml
2级:classpath:config/application.yml
3级:file :application.yml
4级:file :config/application.yml

3、yaml 或 yml 配置文件没有提示

image.png

二、yaml格式

1、含义

YAML(YAML Ain’t Markup Language) 一种数据序列化格式

2、优点

-容易阅读
-容易与脚本语言交互
-以数据为核心,重数据轻格式
image.png image.pngimage.png

3、扩展名

.yml (主流)
.yaml

4、语法规则

  1. 大小写敏感
  2. 属性层级关系使用多行描述, 每行结尾使用冒号结束
  3. 使用缩进表示层级关系,同层级左侧对齐,只允许使用空格(不允许使用Tab键)
  4. 空格的个数并不重要,只要保证同层级的左侧对齐即可。
  5. 属性值前面添加空格(属性名与属性值之间使用冒号+空格作为分隔)
  6. 表示注释

    三、yaml格式读取配置文件数据

    ```yaml lesson: springBoot

server: port: 80

enterprise: name: zhouGe age: 21 tel: 18688883333 subjects:

  • Java
  • 前端
  • 大数据 ```

1、@Value 注解读取数据

  1. package com.example.controller;
  2. import com.example.pojo.User;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.beans.factory.annotation.Value;
  5. import org.springframework.core.env.Environment;
  6. import org.springframework.web.bind.annotation.PathVariable;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8. import org.springframework.web.bind.annotation.RestController;
  9. @RestController
  10. @RequestMapping("/user")
  11. public class UserController {
  12. //使用@Variable注解读取数据
  13. @Value("${lesson}")
  14. private String lesson;
  15. @Value("${server.port}")
  16. private String port ;
  17. @Value("${enterprise.subjects[0]}")
  18. private String subject ;
  19. @RequestMapping("/{id}")
  20. public String getUserId(@PathVariable("id") Integer id){
  21. System.out.println("id======>"+id);
  22. System.out.println("===========使用@Value注解读取数据=============");
  23. System.out.println(lesson);
  24. System.out.println(port);
  25. System.out.println(subject);
  26. return "readData";
  27. }
  28. }

2、Envrioment 环境对象读取数据

  1. package com.example.controller;
  2. import com.example.pojo.User;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.beans.factory.annotation.Value;
  5. import org.springframework.core.env.Environment;
  6. import org.springframework.web.bind.annotation.PathVariable;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8. import org.springframework.web.bind.annotation.RestController;
  9. @RestController
  10. @RequestMapping("/user")
  11. public class UserController {
  12. //使用Environment 对象读取数据
  13. @Autowired
  14. private Environment environment;
  15. @RequestMapping("/{id}")
  16. public String getUserId(@PathVariable("id") Integer id){
  17. System.out.println("id======>"+id);
  18. System.out.println("===========使用Environment读取数据=============");
  19. System.out.println(environment.getProperty("lesson"));
  20. System.out.println(environment.getProperty("server.port"));
  21. System.out.println(environment.getProperty("enterprise.subjects[1]"));
  22. return "readData";
  23. }
  24. }


3、自定义对象读取数据

SpringBoot 还提供了将配置文件中的数据封装到我们自定义的实体类对象中的方式。具体操作如下:

  1. 1. 将实体类 bean 的创建交给 Spring 管理。
  2. 1. 在类上添加 @Component 注解
  3. 1. **使用 @ConfigurationProperties 注解表示加载配置文件 **
  4. 1. 在该注解中也可以使用 prefix 属性指定只加载指定前缀的数据
  5. 1. Controller 中进行注入
  1. package com.example.pojo;
  2. import lombok.Data;
  3. import org.springframework.boot.context.properties.ConfigurationProperties;
  4. import org.springframework.stereotype.Component;
  5. @Data
  6. @Component
  7. @ConfigurationProperties(prefix = "enterprise")
  8. public class User {
  9. private String name ;
  10. private Integer age ;
  11. private String tel ;
  12. private String[] subjects ;
  13. }
  1. package com.example.controller;
  2. import com.example.pojo.User;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.beans.factory.annotation.Value;
  5. import org.springframework.core.env.Environment;
  6. import org.springframework.web.bind.annotation.PathVariable;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8. import org.springframework.web.bind.annotation.RestController;
  9. @RestController
  10. @RequestMapping("/user")
  11. public class UserController {
  12. //使用自定义对象读取数据
  13. @Autowired
  14. private User user;
  15. @RequestMapping("/{id}")
  16. public String getUserId(@PathVariable("id") Integer id){
  17. System.out.println("id======>"+id);
  18. System.out.println("===========使用自定义对象读取数据=============");
  19. System.out.println(user);
  20. return "readData";
  21. }
  22. }

四、多环境配置

1、yaml版

image.png

  1. #需要启用的环境
  2. # --- 代表分割
  3. spring:
  4. profiles:
  5. active: dev
  6. ---
  7. #开发环境
  8. spring:
  9. profiles: dev
  10. server:
  11. port: 80
  12. ---
  13. #生产环境
  14. spring:
  15. profiles: pro
  16. server:
  17. port: 81
  18. ---
  19. #测试环境
  20. spring:
  21. profiles: test
  22. server:
  23. port: 82

2、properties版

image.png

  1. spring.profiles.active=dev
  1. server.port=8080
  1. server.port=8081
  1. server.port=8082

3、命令行配置

a、配置启动环境

  1. maven -clean项目
  2. maven-package项目
  3. 右键open in exploer
  4. cmd ```java java -jar xxx.jar —spring.profiles.active=dev

// —spring.profiles.active=dev 此处内容根据application.yml来书写

  1. ![image.png](https://cdn.nlark.com/yuque/0/2022/png/28229185/1653981411712-8352549e-9f4b-47b4-838a-ab2638eb1358.png#clientId=u50e76812-cd61-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=819&id=u74b2afe4&margin=%5Bobject%20Object%5D&name=image.png&originHeight=1024&originWidth=1497&originalType=binary&ratio=1&rotation=0&showTitle=false&size=192619&status=done&style=none&taskId=uc4b88417-e8c0-440e-b78e-55b9118b390&title=&width=1197.6)<br />![image.png](https://cdn.nlark.com/yuque/0/2022/png/28229185/1653981791082-8d651663-b3f1-489b-b2df-6d528fb892bf.png#clientId=u50e76812-cd61-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=579&id=ue5401464&margin=%5Bobject%20Object%5D&name=image.png&originHeight=724&originWidth=1016&originalType=binary&ratio=1&rotation=0&showTitle=false&size=95136&status=done&style=none&taskId=u08999c8e-7af8-41ed-8793-4530802b1dc&title=&width=812.8)<br />![image.png](https://cdn.nlark.com/yuque/0/2022/png/28229185/1653981685765-26e0ffc8-49a9-4deb-8d0a-f74c0c22f7a6.png#clientId=u50e76812-cd61-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=709&id=u0b7f5d17&margin=%5Bobject%20Object%5D&name=image.png&originHeight=886&originWidth=1838&originalType=binary&ratio=1&rotation=0&showTitle=false&size=162792&status=done&style=none&taskId=ud2a9d219-a42f-4836-a837-537dca4853c&title=&width=1470.4)
  2. <a name="Anmzj"></a>
  3. ### b、配置端口
  4. ```java
  5. java -jar xxxx.jar --server.port=88

image.png

c、混合使用

  1. java -jar xxx.jar --spring.profiles.active=test --server.port=88

image.png

4、多环境开发兼容问题(冲突)

含义:maven和springBoot配置环境产生冲突,此时该以谁的配置为主?

  1. #配置需要使用的环境 ${profile.active}读取maven中的环境 但是需要maven插件支持
  2. spring:
  3. profiles:
  4. active: ${profile.active}
  5. ---
  6. #开发环境
  7. spring:
  8. profiles: dev
  9. server:
  10. port: 80
  11. ---
  12. #生产环境
  13. spring:
  14. profiles: pro
  15. server:
  16. port: 81
  17. ---
  18. #测试环境
  19. spring:
  20. profiles: test
  21. server:
  22. port: 82
  1. <profiles>
  2. <!--开发环境-->
  3. <profile>
  4. <id>dev</id>
  5. <properties>
  6. <!--与application.yml 中的相对应-->
  7. <profile.active>dev</profile.active>
  8. </properties>
  9. <!--设置当前环境为默认环境-->
  10. <activation>
  11. <activeByDefault>true</activeByDefault>
  12. </activation>
  13. </profile>
  14. <!--生产环境-->
  15. <profile>
  16. <id>pro</id>
  17. <properties>
  18. <!--与application.yml 中的相对应-->
  19. <profile.active>pro</profile.active>
  20. </properties>
  21. </profile>
  22. <!--测试环境-->
  23. <profile>
  24. <id>test</id>
  25. <properties>
  26. <!--与application.yml 中的相对应-->
  27. <profile.active>test</profile.active>
  28. </properties>
  29. </profile>
  30. </profiles>
  1. <!--maven 读取资源文件插件支持,依靠它 才可以读取application.yml 中的配置,并进行解析-->
  2. <plugin>
  3. <groupId>org.apache.maven.plugins</groupId>
  4. <artifactId>maven-resources-plugin</artifactId>
  5. <version>3.2.0</version>
  6. <!--设置-->
  7. <configuration>
  8. <!--编码设置-->
  9. <encoding>UTF-8</encoding>
  10. <!--maven占位符 ${} 读取里面的内容}-->
  11. <useDefaultDelimiters>true</useDefaultDelimiters>
  12. </configuration>
  13. </plugin>

5、配置文件优先级

a、开发时

  1. server:
  2. port: 80
  1. server:
  2. port: 81

image.png

b、测试时

image.png
image.png
image.png