1. 程式人生 > 其它 >Spring Boot配置檔案---約定大於配置

Spring Boot配置檔案---約定大於配置

技術標籤:SpringBoot

   Spring Boot 預設全域性配置檔案,分別是properties檔案和yml檔案。他們主要作用是修改Spring Boot的自動配置的預設值,相對於properties檔案而言,更多人喜歡使用yml的配置檔案。在此之前我們一般都是使用xml的形式來完成我們的配置。xml是以標籤的形式存在,而properties則是k=v的形式存在,相對於前兩者而言, yml是以資料為中心,比json、xml更適合做配置檔案,yml的出現更受大眾的青睞。本文重點說說yml的魅力之處。以前我們都覺得配置就是一個專案的核心,現在只要記住一句話就OK啦!“約定大於配置”

一、.xml的語法,是一個標記文件(以標籤的形式存在,寫起來比較繁瑣)

<?xml version="1.0" encoding="UTF-8"?>
<beans
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context=
"http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation=" http://www.springframework.org/schema/
tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd "> <!-- 開啟註解 --> <context:annotation-config /> <!-- 開啟定時任務 --> <task:annotation-driven/> <!-- 自動掃描(service),mapper(dao) --> <context:component-scan base-package="com.gx.mapper,com.gx.service,com.gx.job"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Service" /> <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository" /> <context:include-filter type="annotation" expression="org.springframework.stereotype.Component"/> </context:component-scan> </beans>

二、.properties的語法 (以k=v的形式存在,寫起來比上面的xml好不了多少)

//修改預設的埠號
server.port=8081
//訪問靜態資源的路徑
spring.resources.static-locations=classpath:/static,classpath:/public,classpath:/resources,classpath:/META-INF/resources
//檔案上傳的最大值
spring.servlet.multipart.max-request-size=10MB
spring.servlet.multipart.max-file-size=10MB

三、.yml 的語法,YAML A Markup Language:是一個標記語言 (以K空格V 的形式存在,寫起來比較簡便)

&emsp; &emsp;“約定大於配置”,我想這句話很多人都聽說了吧!其實這麼說也有它的道理的。

1、基本語法:

(1)、屬性和值的大小字母都是比較敏感;
(2)、k:空格v:表示一對鍵值對(空格必須要有);
(3)、以空格的縮排來控制控制層級關係;只要是左對齊的一列資料,都是同一個級;否則會出現資料混亂

      server:
           port: 8090
           path: /hello

2、值的寫法:

(1)、在字串的賦值時,不需要新增單引號或者雙引號。如果裡添加了單(雙)引號,則特殊字元以字串的形式輸出。字串預設不用加上單引號或者雙引號。

正確 :name:   小明 \n 小紅:輸出:小明 換行 小紅
錯誤:name:   ‘小明 \n 小紅’:輸出;小明\n 小紅 

3、物件、Map的寫法:(物件是以屬性和值,Map是鍵值對)

//第一種寫法

Friends: {lastName: xiaozhang, age: 20}

//第二中寫法。在寫的時候需要注意:在下一行來寫物件和屬性的關係,記得縮排 。物件是以K:(空格)V的形式

friends:
       lastName: xiaozhang
       Age: 20

4、陣列(List)的寫法

//-(空格)值表示陣列中的某一個的元素

Pets:
  - cat
  -dog
  -pig

5、以學生類為例

Student類

@Component  //將此Javabean放入spring容器
@ConfigurationProperties(prefix="student")  //通過他批量注入
public class Student {

 @Value(“ls”)  //修改其中的值
private String name;/* 姓名 */
 @Value(33)
 private int age;/* 年齡 */
 private boolean sex;/* 性別 */
 private Date birthday;/* 出生日期 */
 private Map<String, Object> location; /* 家庭地址 */
 private String[] hobbies; /* 愛好 */
 private List<String> skills; /* 技能 */
 private Pet pet;/* 寵物 *

//生成get和set
//生成toString()
}

yml賦值

student:
   name: ls
   age: 24
   sex: true
   birthday: 2019/2/12
   location: {province: 廣東,city: 廣州,zone: 天河區}
   hobbies:
        - 籃球
        - 音樂
   skills:
       - 程式設計
       - 金融
   pet: 
     nickName: wangcai
     strain: hsq

第一次輸出的結果:
在這裡插入圖片描述

修改後輸出結果:
在這裡插入圖片描述

   通過上面的結論:一次又一次證明了.yml的配置,符合了約定大於配置。如果裡的寫法不符合約定就會出錯。雖然.yml的配置寫法確實減少了不少,但我們一定要遵守我們的約定。