筆記四 SpringBoot的配置檔案

2021-10-01 07:20:33 字數 2234 閱讀 3642

四、 springboot的配置檔案

4.1 springboot配置檔案型別

4.1.1 springboot配置檔案型別和作用

4.1.2.1 yml配置檔案簡介

yml檔案格式是yaml編寫的檔案格式,yaml是一種直觀的能夠被電腦識別的資料序列化格式,容易和指令碼語言互動的,可以被支援yaml庫的不同的程式語言程式匯入。yml檔案是以資料為核心的,比傳統的xml方式更加簡潔。yml副檔名可以使用.yml或.yaml。

4.1.2.2 yml配置檔案的語法

配置普通資料

name: haohao
配置物件資料

person:

name: haohao

age: 31

addr: beijing

#或者 行內物件配置

person:

配置map資料

同上面的物件寫法

map:

key1: value1

key2: value2

配置陣列(list、set)資料

city:

- beijing

- tianjing

#或者city: [beijing,tianjing]

#集合中的元素是物件形式

student:

- name: zhangsan

age: 18

score: 100

- name: lisi

age: 28

score: 88

student: [,]

4.1.3 springboot配置資訊的查詢

上面提及過,springboot的配置檔案,主要的目的就是對配置資訊進行修改的,但在配置時的key去**查詢,可以查閱springboot的官方文件

文件url:

4.2 配置檔案與配置類的屬性對映方式

4.2.1 使用註解@value對映

我們可以通過@value註解將配置檔案中的值對映到乙個spring管理的bean的字段上

person:

name: zhangsan

age: 18

person:

name: zhangsan

age: 18

實體bean**如下:

@controller

public class quick2controller ")

private string name;

@value("$")

private string addr;

@responsebody

public string quick2()

}

4.2.2 使用註解@configurationproperties對映

通過註解@configurationproperties(prefix=「配置檔案中的key的字首」)可以將配置檔案中的配置自動與實體進行對映

實體bean**如下:

@controller

@configurationproperties(prefix = "person")

public class quickstartcontroller

public void setname(string name)

public integer getage()

public void setage(integer age)

@responsebody

public string quick()

}

pom.xml上加個依賴,就不會報warning了。

org.springframework.boot

spring-boot-configuration-processor

true

springboot的logback的配置檔案

之前敲出來乙個 logback 的配置檔案,防止以後找起來麻煩在這裡存個檔 修改了日誌輸出的內容 設定預設級別為 info 正常日誌 trace 到 warn 輸出到單獨的資料夾 異常日誌 error 輸出到單獨的資料夾 日誌檔案按天進行儲存,每天都會新建日誌檔案 日誌檔案儲存在當前專案目錄下 配置...

SpringBoot新增Swagger配置

swagger 是乙個規範和完整的框架,用於生成 描述 呼叫和視覺化 restful 風格的 web 服務。總體目標是使客戶端和檔案系統作為伺服器以同樣的速度來更新。檔案的方法 引數 模型緊密整合到伺服器端的 允許api來始終保持同步。swagger 讓部署管理和使用功能強大的api從未如此簡單。1...

SpringBoot系列(四)資源檔案屬性配置

自定義資源檔案 1 首先在pom.xml中新增依賴,不新增的話讀取不到 org.springframework.boot spring boot configuration processor true 2 繫結資源檔案 configuration configurationproperties p...