SpringBoot 配置檔案繫結類

2021-10-03 04:53:42 字數 1031 閱讀 4435

以map資料型別為例:

例如我們在resources.config下有乙個exception-code.properties配置檔案

lyn.codes[

10000

] = this is error1

lyn.codes[

10001

] = this is error2

讀取配置檔案的**如下:

@configurationproperties

(prefix =

"lyn"

)@propertysource

(value =

"classpath:config/exception-code.properties"

)@component

public

class

exceptioncodeconfiguration

private map

codes =

newhashmap

<

>()

;// 這裡欄位的命名必須與配置檔案內的變數名相同,springboot會進行自動繫結

public string getmessage

(int code)

}

configurationproperties註解是宣告配置檔案的字首,即"lyn.codes"中的"lyn"

propertysource註解,宣告配置檔案的路徑

在需要使用資料的地方,直接

@autowired

private exceptioncodeconfiguration exceptioncodeconfiguration;

然後使用exceptioncodeconfiguration.getmessage()即可獲取相應的值

Spring Boot配置檔案

方式 示例 檔案路徑 classpath或者classpath的 config目錄下 檔案內容 全域性通用配置 方式 通過environment獲取 示例 system.out println ctx.getenvironment getproperty env 方式一 configurationp...

springboot配置檔案

配置檔案的作用 修改springboot自動配置的預設值 yaml yaml ain t markup language yaml a markup language 是乙個標記語言 yaml isn t markup language 不是乙個標記語言 標記語言 以前的配置檔案大都是xml檔案,y...

springboot 配置檔案

1.配置檔案 系統配置 自定義配置 server host 空格 value windows檔案路徑,包含 可以直接寫。以空格個數區分層次 logging level root trace file log.txt server.host value 顯示單引號 包 value 包 顯示雙引號 包 ...