Spring中的@ConfigurationProperties在方法上的使用詳解
前言
在學習spring的時候,@ConfigurationProperties應該經(jīng)常被使用到,作用在類上的時候,將該類的屬性取值 與配置文件綁定,并生成配置bean對象,放入spring容器中,提供給其他地方使用。
在工作中,或者看spring內(nèi)部代碼的時候,無意發(fā)現(xiàn)@ConfigurationProperties居然還可以用在方法上,點開@ConfigurationProperties注解的時候,我們發(fā)現(xiàn):
// targer:可作用在類、方法上 @Target({ ElementType.TYPE, ElementType.METHOD }) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface ConfigurationProperties { }
似乎平時我們都是用在類上的,因此特意寫這篇文章記錄一下,記錄@ConfigurationProperties的使用方式。
先說結論
- @ConfigurationProperties無論是使用在類上,或用在方法上,其本質都是將某個配置類的屬性值與配置文件application.yml中內(nèi)容進行綁定。
- 其使用場景用如下三種:
- @ConfigurationProperties + @Component + 在類上使用的場景
- @ConfigurationProperties + @EnableConfigurationProperties 在類上使用的場景
- @ConfigurationProperties + @Bean 在方法上使用的場景
代碼解釋
@Component + @ConfigurationProperties
在類上使用@ConfigurationProperties效果:
@Component @ConfigurationProperties(prefix = "test.config") @Setter @ToString public class TestConfigDemo { private String username; private String pwd; } // yml test: config: username: test-config-username pwd: test-config-pwd // 測試代碼 @Component public class TestConfigAppRunner implements ApplicationRunner { @Autowired TestConfigDemo testConfigDemo; @Override public void run(ApplicationArguments args) throws Exception { System.out.println(testConfigDemo); } }
從上述可看出@ConfigurationProperties的作用,而@ConfigurationProperties的使用則是在配置類上需要有set方法、以及是需要@Component。
@EnableConfigurationProperties + @ConfigurationProperties
這里不做過多說明
@Bean + @ConfigurationProperties
直接看代碼:
// 該配置類上 無 任何spring的注解 @Setter @ToString public class TestConfigDemo { private String username; private String pwd; }
// 準備個@Configuration 配置類 @Configuration public class ConfigurationDemo { // 這里將TestConfigDemo 配置類 生成bean對象 // 此時:TestConfigDemo里面的屬性將與配置文件yml內(nèi)容進行綁定 @Bean @ConfigurationProperties(prefix = "test.config") public TestConfigDemo testConfigDemo(){ return new TestConfigDemo(); } }
@Component public class TestConfigAppRunner implements ApplicationRunner { @Autowired TestConfigDemo testConfigDemo; @Override public void run(ApplicationArguments args) throws Exception { System.out.println(testConfigDemo); } }
看到上述的例子,想必大家內(nèi)心此時的想法是:@ConfigurationProperties使用在方法上的效果,跟使用在類上的完全沒任何區(qū)別,只是將@ConfigurationProperties的位置換了一個地方,并無太大區(qū)別吧。
但其出現(xiàn)必然有其存在意義: 個人覺得,其作用就是,如果項目中有個@Configuration這種類的話,可以把config配置類的注解內(nèi)容等都放入該類中,達到一種配置好管理的效果,之后如果要改什么內(nèi)容,只要看@Configuration這種類即可,無需到處找。
到此這篇關于Spring中的@ConfigurationProperties在方法上的使用詳解的文章就介紹到這了,更多相關@ConfigurationProperties的使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringMVC打印請求參數(shù)和響應數(shù)據(jù)最優(yōu)方案
項目中經(jīng)常需要打印http請求的參數(shù)和響應數(shù)據(jù),本文給大家講解如何在SpringMVC打印請求參數(shù)和響應數(shù)據(jù)最優(yōu)方案,感興趣的朋友跟隨小編一起看看吧2023-07-07Java中數(shù)據(jù)庫常用的兩把鎖之樂觀鎖和悲觀鎖
這篇文章主要介紹了數(shù)據(jù)庫常用的兩把鎖之樂觀鎖和悲觀鎖,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-07-07異常解決SpringBoot項目啟動卡住,無任何異常信息問題
這篇文章主要介紹了異常解決SpringBoot項目啟動卡住,無任何異常信息問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03Kotlin Coroutines執(zhí)行異步加載示例詳解
這篇文章主要給大家介紹了關于Kotlin Coroutines執(zhí)行異步加載的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。2018-01-01MyBatisPlus-QueryWrapper多條件查詢及修改方式
這篇文章主要介紹了MyBatisPlus-QueryWrapper多條件查詢及修改方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06