前言:有時候在sping boot專案中需要自定義屬性。又想在配置檔案(*.properties)中顯示提示時。
測試環境:sping boot2x + m**en + lombok
準備測試的配置類:
@data
@configuration
@configurationproperties(prefix = "animal.default")
public class animal
使用@configurationproperties(prefix = "animal.default")
自定義乙個字首。使用預設配置檔案
我們可以看到這個時候在配置檔案中輸入是沒有關於「animal.default」的任何提示。
現在我們通過新增配置則可以讓它有顯示。
1. 在啟動類上新增@enableconfigurationproperties
註解。如下所示:
@enableconfigurationproperties
//@enableconfigurationproperties() 可以指定類,也可以不指定
public static void main(string args)
}2. 使用m**en工具依次執行 clean 和 package 命令。在執行命令後可以看到下圖:
現在我們再去配置檔案中測試會發現已經有提示功能。如下圖:
SpringBoot自定義屬性配置讀取
springboot讀取配置檔案,將配置檔案內容注入到entity或者配置欄位中。2.定義乙個entity,給出set,get方法。加上註解 component configurationproperties prefix girl 可看見註解來自於boot。這樣便可以注入屬性,進行相關預設值的設定...
spring boot 自定義配置屬性的各種方式
spring boot的配置資訊支援按照變數 類 複雜類的方式進行定義。my.prop.男 第二步 應用類上通過註解獲取屬性 component public class myclass private string name value private int my.prop.男 第二部 定義屬性...
SpringBoot 配置自定義屬性並使用
在.properties檔案中設定需要的引數 test.name 測試 到需要用到這個屬性的地方用 value註解為定義的字段注入值 value註解作用該註解作用的作用是將我們配置檔案的屬性讀出來 寫法主要有兩種 value 和 value 想要深入了解請轉隔壁 spring 註解 value詳解 ...