Java中@ConfigurationProperties實現(xiàn)自定義配置綁定問題分析
@ConfigurationProperties使用
創(chuàng)建一個類,類名上方注解,配置prefix屬性,如下代碼:
@ConfigurationProperties( prefix = "hello.properties" ) public class MyProperties { private String myKey; private List<String> stringList; private Duration duration; public String getMyKey() { return myKey; } public void setMyKey(String myKey) { this.myKey = myKey; } public List<String> getStringList() { return stringList; } public void setStringList(List<String> stringList) { this.stringList = stringList; } public Duration getDuration() { return duration; } public void setDuration(Duration duration) { this.duration = duration; } @Override public String toString() { return "MyProperties{" + "myKey='" + myKey + '\'' + ", stringList=" + stringList + ", duration=" + duration + '}'; } }
prefix
屬性是配置文件里的前綴,即配置文件中以前綴 + 變量名
的形式配置一條記錄,來對應(yīng)類中的一個變量,如下:
hello.properties.myKey=hello hello.properties.duration=20s hello.properties.string-list[0]=Acelin hello.properties.string-list[1]=nice
@ConfigurationProperties特點
寬松綁定
如下配置都是可以被識別綁定的:
hello.properties.myKey=hello hello.properties.mykey=hello hello.properties.my-key=hello hello.properties.my_key=hello hello.properties.MY_KEY=hello hello.properties.MY-KEY=hello
支持復(fù)雜屬性類型
支持從配置參數(shù)中解析 durations (持續(xù)時間)
hello.properties.duration=20s
List 和 Set
hello.properties.string-list[0]=Acelin hello.properties.string-list[1]=nice
激活@ConfigurationProperties
通過@EnableConfigurationProperties
如果一個配置類只單單用@ConfigurationProperties注解,那么在IOC容器中是獲取不到properties 配置文件轉(zhuǎn)化的bean。我們可以在想要使用該配置類的類上注解@EnableConfigurationProperties,并配置相關(guān)的類,即可拿到該裝配好配置的類了。如下所示:
通過@ConfigurationPropertiesScan
該注解有點類似與@CompomentScan注解掃描@Compoment注釋的類相似,也是用來掃描項目中@ConfigurationProperties注解的類,并注入spring容器中。只需將該注解注釋于項目啟動類上即可
其實@ConfigurationProperties更多的作用是將配置文件中的配置與類中變量對應(yīng)上來,而上述兩種方式是告訴Spring容器要把這個有配置特性的Bean在程序啟動的時候給創(chuàng)建出來。那談到的創(chuàng)建Bean,我們就會想到Spring創(chuàng)建Bean的各種方式,這些方式的同樣能夠激活@ConfigurationProperties,詳細請看Spring Boot創(chuàng)建Bean的幾種方式
@ConfigurationProperties與@Value對比
- | @ConfigurationProperties | @Value |
---|---|---|
功能 | 批量注入配置文件中的屬性 | 一個個指定 |
松散綁定(松散語法) | 支持 | 不支持 |
SpEL | 不支持 | 支持 |
JSR303數(shù)據(jù)效驗 | 支持 | 不支持 |
復(fù)雜類型封裝 | 支持 |
使用 Spring Boot Configuration Processor 完成自動補全
當(dāng)我們在配置文件中寫官方支持的配置的時候,我們都會發(fā)現(xiàn)的有自動補全配置的一個功能,那怎么也讓我們自己的配置也實現(xiàn)這種功能呢?
其實當(dāng)你用這個注解的時候,IDE是會提示你這一點的,她會在文件的上方提示你要可以配置自動補全的功能:
實現(xiàn)的方式就是項目導(dǎo)入依賴:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> </dependency>
然后重新編譯或運行項目:
項目會生產(chǎn)一個json文件
然后能夠?qū)崿F(xiàn)自動提示補全配置項的功能了
到此這篇關(guān)于Java中@ConfigurationProperties實現(xiàn)自定義配置綁定問題分析的文章就介紹到這了,更多相關(guān)@ConfigurationProperties自定義配置綁定內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot使用Kaptcha實現(xiàn)驗證碼的生成與驗證功能
這篇文章主要介紹了SpringBoot使用Kaptcha實現(xiàn)驗證碼的生成與驗證功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-03-03Java編程通過匹配合并數(shù)據(jù)實例解析(數(shù)據(jù)預(yù)處理)
這篇文章主要介紹了Java編程通過匹配合并數(shù)據(jù)實例解析(數(shù)據(jù)預(yù)處理),分享了相關(guān)代碼示例,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下2018-01-01SpringBoot如何配置文件properties和yml
這篇文章主要介紹了SpringBoot如何配置文件properties和yml問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08常用的Java數(shù)據(jù)結(jié)構(gòu)知識點匯總
這篇文章主要介紹了常用的Java數(shù)據(jù)結(jié)構(gòu)知識點匯總,數(shù)據(jù)結(jié)構(gòu)分線性數(shù)據(jù)結(jié)構(gòu)和非線性數(shù)據(jù)結(jié)構(gòu),下面對此作詳細介紹,需要的小伙伴可以參考一下,希望對你的學(xué)習(xí)或工作有所幫助2022-03-03Mybatis-Plus實體類注解方法與mapper層和service層的CRUD方法
CRUD是指在做計算處理時的增加(Create)、讀取查詢(Retrieve)、更新(Update)和刪除(Delete)幾個單詞的首字母簡寫。主要被用在描述軟件系統(tǒng)中DataBase或者持久層的基本操作功能,下面讓我們一起看看吧2022-03-03