springboot使用@ConfigurationProperties實(shí)現(xiàn)自動(dòng)綁定配置參數(shù)屬性
@ConfigurationProperties使用
@ConfigurationProperties是 springboot提供用于將配置文件中的屬性值映射到 Java bean對(duì)象上。通過(guò)使用該注解,我們可以方便地將屬性文件中的值綁定到一個(gè)實(shí)例化的類(lèi)對(duì)象上,從而方便地在應(yīng)用程序中使用這些屬性。
@ConfigurationProperties注解有一個(gè)參數(shù)prefix用來(lái)指定屬性公共前綴
@Configuration @ConfigurationProperties(prefix = "myconfig") @Data public class MyConfigProperty { private int port; //找不到的屬性不會(huì)注入 private String hhh; }
一般作為屬性注入對(duì)象,首先定義成一個(gè)@Configuration。然后使用@ConfigurationProperties指定關(guān)聯(lián)屬性的前綴。
這樣如果配置文件中有myconfig.port的值就會(huì)自動(dòng)綁定到MyConfigProperty類(lèi)的port屬性上。前提要有對(duì)應(yīng)的set方法。
除了在類(lèi)上標(biāo)注外,還可以在@Bean方法上
@Configuration public class MyConfigByMethod { @Bean @ConfigurationProperties(prefix = "myconfig") public MyConfig myConfig(){ return new MyConfig(); } }
觀察spring的源碼,還會(huì)使用@EnableConfigurationProperties引入被@ConfigurationProperties修飾的bean
框架自動(dòng)裝配解析
在springboot框架自動(dòng)裝配中有一個(gè)內(nèi)置的用來(lái)處理@ConfigurationProperties注解的配置類(lèi)ConfigurationPropertiesAutoConfiguration,該配置類(lèi)引入@EnableConfigurationProperties,然后間接引入EnableConfigurationPropertiesRegistrar,EnableConfigurationPropertiesRegistrar在configuration初始化的時(shí)候會(huì)調(diào)用其registerBeanDefinitions()方法進(jìn)行配置類(lèi)中擴(kuò)展beanDef的加載。
EnableConfigurationPropertiesRegistrar#registerBeanDefinitions
public void registerBeanDefinitions(AnnotationMetadata metadata, BeanDefinitionRegistry registry) { registerInfrastructureBeans(registry); registerMethodValidationExcludeFilter(registry); ConfigurationPropertiesBeanRegistrar beanRegistrar = new ConfigurationPropertiesBeanRegistrar(registry); getTypes(metadata).forEach(beanRegistrar::register); } private Set<Class<?>> getTypes(AnnotationMetadata metadata) { return metadata.getAnnotations().stream(EnableConfigurationProperties.class) .flatMap((annotation) -> Arrays.stream(annotation.getClassArray(MergedAnnotation.VALUE))) .filter((type) -> void.class != type).collect(Collectors.toSet()); } static void registerInfrastructureBeans(BeanDefinitionRegistry registry) { ConfigurationPropertiesBindingPostProcessor.register(registry); BoundConfigurationProperties.register(registry); }
這里主要會(huì)完成兩件事件:
1、registerInfrastructureBeans會(huì)將ConfigurationPropertiesBindingPostProcessor注冊(cè)到容器中,這是一個(gè)后置處理其,屬性的賦值會(huì)在其后置方法里完成。
2、注冊(cè)合適的ConfigurationProperties類(lèi)型bean, 當(dāng)前metadata是正在初始化的Configuration類(lèi),然后從其注解上獲取帶有EnableConfigurationProperties注解作為bean定義加載到容器中。
來(lái)看幾個(gè)自動(dòng)裝配的例子:
ServletWebServerFactoryAutoConfiguration
- ServletWebServerFactoryAutoConfiguration上帶有@EnableConfigurationProperties(ServerProperties.class)注解,則ServerProperties會(huì)作為一個(gè)bean進(jìn)行處理。
- ServerProperties上配置有@ConfigurationProperties(prefix = “server”, ignoreUnknownFields = true),我們場(chǎng)景的server.port屬性就會(huì)注入到ServerProperties.port屬性上。
DataSourceAutoConfiguration
@EnableConfigurationProperties(DataSourceProperties.class) public class DataSourceAutoConfiguration {} @ConfigurationProperties(prefix = "spring.datasource") public class DataSourceProperties{}
DataSourceProperties會(huì)被做為一個(gè)bean加載,"spring.datasource"下的屬性會(huì)注入到DataSourceProperties屬性中。
ConfigurationPropertiesBindingPostProcessor
ConfigurationPropertiesBindingPostProcessor是一個(gè)bean后置處理器,在bean實(shí)例化后會(huì)調(diào)用其后置方法
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { bind(ConfigurationPropertiesBean.get(this.applicationContext, bean, beanName)); return bean; }
ConfigurationPropertiesBean.get()方法會(huì)判斷當(dāng)前bean是否有ConfigurationProperties注解,如果有會(huì)進(jìn)行對(duì)應(yīng)的屬性綁定。
最后使用org.springframework.boot.context.properties.bind.Binder類(lèi)進(jìn)行屬性綁定。
這里ConfigurationProperties會(huì)有兩部分,一是框架通過(guò)autoConfig自動(dòng)裝配的,一種是我們自己顯示使用@ConfigurationProperties修飾的bean。
這里看到我們?cè)谧远x@ConfigurationProperties時(shí)候不一定非用@Configuration進(jìn)行修飾,只要當(dāng)前類(lèi)能被解析成一個(gè)bean,都會(huì)調(diào)用該后置方法進(jìn)行對(duì)應(yīng)配置屬性的賦值。
屬性元數(shù)據(jù)信息
可配置的屬性在每個(gè)jar包META-INFO/spring-configuration-metadata.json文件。這樣一般在IDEA配置application文件時(shí)候都能根據(jù)該文件里的元數(shù)據(jù)信息進(jìn)行提示配置。
例如server.port配置在spring-boot-autoconfigure.jar包中
{ "name": "server.port", "type": "java.lang.Integer", "description": "Server HTTP port.", "sourceType": "org.springframework.boot.autoconfigure.web.ServerProperties", "defaultValue": 8080 },
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- SpringBoot中的@ConfigurationProperties注解的使用
- SpringBoot中@ConfigurationProperties自動(dòng)獲取配置參數(shù)的流程步驟
- Springboot之@ConfigurationProperties注解解讀
- Springboot中@ConfigurationProperties輕松管理應(yīng)用程序的配置信息詳解
- SpringBoot中@Value獲取值和@ConfigurationProperties獲取值用法及比較
- springboot中@ConfigurationProperties無(wú)效果的解決方法
- SpringBoot中的@ConfigurationProperties注解解析
相關(guān)文章
SpringBootWeb?入門(mén)了解?Swagger?的具體使用
這篇文章主要介紹了SpringBootWeb?入門(mén)了解?Swagger?的具體使用,Swagger?框架可以根據(jù)已經(jīng)實(shí)現(xiàn)的方法或者類(lèi),通過(guò)頁(yè)面的方式直觀清晰的查看或者進(jìn)行測(cè)試該方法,需要的朋友可以參考下2024-08-08SpringBoot整合screw實(shí)現(xiàn)自動(dòng)生成數(shù)據(jù)庫(kù)設(shè)計(jì)文檔
使用navicat工作的話,導(dǎo)出的格式是excel不符合格式,還得自己整理。所以本文將用screw工具包,整合到springboot的項(xiàng)目中便可以自動(dòng)生成數(shù)據(jù)庫(kù)設(shè)計(jì)文檔,非常方便,下面就分享一下教程2022-11-11package打包一個(gè)springcloud項(xiàng)目的某個(gè)微服務(wù)報(bào)錯(cuò)問(wèn)題
這篇文章主要介紹了package打包一個(gè)springcloud項(xiàng)目的某個(gè)微服務(wù)報(bào)錯(cuò)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07springboot項(xiàng)目攔截前端請(qǐng)求中的特殊字符串(解決方案)
springboot項(xiàng)目中,需要對(duì)前端請(qǐng)求數(shù)據(jù)進(jìn)行過(guò)濾,攔截特殊字符,本文通過(guò)實(shí)例代碼給大家分享完美解決方案,感興趣的朋友一起看看吧2023-10-10關(guān)于springboot 配置文件中屬性變量引用方式@@解析
這篇文章主要介紹了關(guān)于springboot 配置文件中屬性變量引用方式@@解析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04小議Java中final關(guān)鍵字使用時(shí)的注意點(diǎn)
final關(guān)鍵字代表著最后、不可改變,無(wú)論是在用final修飾類(lèi)、修飾方法還是修飾變量時(shí),都要注意內(nèi)存分配的問(wèn)題.這里來(lái)小議Java中final關(guān)鍵字使用時(shí)的注意點(diǎn):2016-06-06SpringBoot配置Email發(fā)送功能實(shí)例
本篇文章主要介紹了SpringBoot配置Email發(fā)送功能實(shí)例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-04-04SpringBoot集成Solr實(shí)現(xiàn)全文檢索功能
solr是一個(gè)現(xiàn)成的全文檢索引擎系統(tǒng), 放入tomcat下可以獨(dú)立運(yùn)行, 對(duì)外通過(guò)http協(xié)議提供全文檢索服務(wù),這篇文章給大家介紹了SpringBoot集成Solr實(shí)現(xiàn)全文檢索功能,文中有詳細(xì)的代碼示例供大家參考,需要的朋友可以參考下2024-03-03Springboot實(shí)現(xiàn)動(dòng)態(tài)定時(shí)任務(wù)流程詳解
通過(guò)重寫(xiě)SchedulingConfigurer方法實(shí)現(xiàn)對(duì)定時(shí)任務(wù)的操作,單次執(zhí)行、停止、啟動(dòng)三個(gè)主要的基本功能,動(dòng)態(tài)的從數(shù)據(jù)庫(kù)中獲取配置的定時(shí)任務(wù)cron信息,通過(guò)反射的方式靈活定位到具體的類(lèi)與方法中2022-09-09