二 SpringBoot - 核心配置文件

1、application.properties 和 application.yml 配置文件格式區別1.1 文件格式application.properties
# 端口號server.port=8096application.yml
# 服務端口server:port: 80961.2 區別

  1. properties的優先級高于yml,同等配置,高優先級會覆蓋低優先級,不同的配置時互補配置(增補 , 不管哪個配置文件中有,都可以生效);
  2. properties的核心語法是:通過 . 作為層級分隔符 , 配置值是用 =,比如 server.port=9096yml的核心語法是:通過層級+縮進的方式,同一給等級 , 縮進是相同的,配置使用key: value方式
    • server:port: 8096#注意值前面必須有空格
  3. 小結:yml格式配置,可以簡化配置內容,層次清晰,更適合作為核心配置文件;
2、自定義配置2.1 配置信息 yml 語法注意:值前面必須有空格;
2.1.1 基本類型數據user:userId: kh96user-Name: gala # 支持松散綁定user_age: 17adult: true # 是否成年salary: 9696.0userTel: 13501020304birthday: 2002/10/11 10:10:10email: kh96@163.com2.1.2數組,List,Setuser: hobbies:# 愛好 list集合- springboot- linux- mysql- ssm- jvaweb- springvloud#行內寫法#hobbies:[springboot,linux,mysql,ssm,jvaweb,springvloud]2.1.3 Mapuser: carMap:# 愛車 map 集合bnm: 寶馬325audi: 奧迪A41benz: 奔馳C200#行內寫法#carMap:{bnm: 寶馬325;audi: 奧迪A41;benz: 奔馳C200}2.1.4 實體參數user:userRole:role-id: R96 ${random.uuid} #${}可以去一些內置的自定義參數role_name: root2.1.5 值的寫法2.1.5.1 單引號:會轉義特殊字符 。
user: msg: '你好!\n小可愛!'輸出:
你好!\n小可愛!2.1.5.2 雙引號:不會轉義字符里的特殊字符,特殊字符仍然是本身的意思
user: msg: "你好!\n小可愛!"輸出:
你好!小可愛!2.2 獲取 配置信息2.2.1 批量自動讀取使用注解@ConfigurationProperties(prefix = "xxx") ,必須配合@Component 注解獲取在核心啟動類上使用 @EnableConfigurationProperties(配置屬性讀取類.class)使用;
特點:支持松散綁定(可以自動識別駝峰 , -,_),支持復雜類型綁定(實體,集合-list,set,array,map等),支持數據格式校驗;
@Component + @ConfigurationProperties(prefix = "user")或@Component+@EnableConfigurationProperties(UserProperties.class) //寫在主啟動類上2.2.1.1 UserProperties@Data@Component //第一個寫法 , 使用普通組件@ConfigurationProperties(prefix = "user")//不能單獨使用,必須配合@EnableConfigurationProperties 或指定為spring容器中的普通組件public class UserProperties {//用戶編號private String userId;//用戶名private String userName;//用戶年齡private Integer userAge;//是否成年private boolean adult;//工資privatedouble salary;//聯系方式private String userTel;//生日@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8") //springMVC將將數據轉成json格式,時間格式規則private Date birthday;//用戶角色private UserRole userRole; //實體參數//愛好private List<String> hobbies;//愛車private Map<String,String> carMap;//郵箱@Email //郵箱格式校驗private String email;}2.2.1.1.2運行結果:
二 SpringBoot - 核心配置文件

文章插圖
2.2.2 單個手動讀取用法:使用注解@Value("${xxx.xxx}");
特點:寫法靈活,可以指定默認值等,但是不支持松散綁定,單個讀取的配置要求指定的讀取屬性key必須和自定義配置一直,否者報錯;
@Component + @ConfigurationProperties(prefix = "user")2.2.2.1 UserProperties@Data@Component@PropertySource(value = "https://www.huyubaike.com/biancheng/classpath:user.properties")//@EnableConfigurationProperties(UserProperties.class) //第二種方式 , 核心啟動類上 , 增加指定開啟自動配置讀取,但是一般不怎么使用,且容易忘記public class UserProperties {//用戶編號@Value("${user.userId}")private String userId;//用戶名@Value("${user.user-Name}")private String userName;//昵稱 @Value("#{userValues.userName}") //獲取的是容器中已有的實體的值 //@Value("#{'xiaoming'}") //可以賦默認值private String niceName;//用戶年齡@Value("${user.user_age}")//@Value("16") //直接賦值private Integer userAge;//是否成年@Value("#{(${user.user_age}>17)?true:false}") //spel 表達式private boolean adult;//工資@Value("#{${user.salary}*10}")//#{} 和 ${}套用privatedouble salary;//聯系方式@Value("${user.userTel}")private String userTel;//生日@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8") //springMVC將將數據轉成json格式,時間格式規則@Value("${user.birthday}")private Date birthday;//用戶角色//@Value("${user.userRole}")//不可以單個手動獲取石參數private UserRole userRole; //實體參數//愛好//@Value("${user.hobbies}") //不可以單個手動獲取復雜參數private List<String> hobbies;//愛車//@Value("${user.carMap}")private Map<String,String> carMap;//郵箱@Email //郵箱格式校驗@Value("${user.email:abc@kgc.com}") //添加默認值,配置信息沒有就使用默認值private String email;}

推薦閱讀