SpringBoot中的@EnableAutoConfiguration注解解析
SpringBoot中的@EnableAutoConfiguration注解
借助@Import的支持,收集和注冊特定場景相關(guān)的bean定義。
@EnableAutoConfiguration也是借助@Import的幫助,將所有符合自動配置條件的bean定義注冊到IoC容器,僅此而已!
@EnableAutoConfiguration會根據(jù)類路徑中的jar依賴為項目進行自動配置,如:添加了spring-boot-starter-web依賴,會自動添加Tomcat和Spring MVC的依賴,Spring Boot會對Tomcat和Spring MVC進行自動配置。
@EnableAutoConfiguration作為一個復合Annotation,其自身定義關(guān)鍵信息如下:
@SuppressWarnings("deprecation") @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @AutoConfigurationPackage @Import(EnableAutoConfigurationImportSelector.class) public @interface EnableAutoConfiguration { ... }
最關(guān)鍵的要屬@Import(EnableAutoConfigurationImportSelector.class),借助EnableAutoConfigurationImportSelector,@EnableAutoConfiguration可以幫助SpringBoot應用將所有符合條件的@Configuration配置都加載到當前SpringBoot創(chuàng)建并使用的IoC容器。
就像一只“八爪魚”一樣,借助于Spring框架原有的一個工具類:SpringFactoriesLoader的支持,@EnableAutoConfiguration可以智能的自動配置功效才得以大功告成!
SpringFactoriesLoader屬于Spring框架私有的一種擴展方案,其主要功能就是從指定的配置文件META-INF/spring.factories加載配置。
從classpath中搜尋所有的META-INF/spring.factories配置文件,并將其中org.springframework.boot.autoconfigure.EnableAutoConfiguration對應的配置項通過反射(Java Refletion)實例化為對應的標注了@Configuration的JavaConfig形式的IoC容器配置類,然后匯總為一個并加載到IoC容器。
到此這篇關(guān)于SpringBoot中的@EnableAutoConfiguration注解解析的文章就介紹到這了,更多相關(guān)@EnableAutoConfiguration注解內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot自動配置@EnableAutoConfiguration過程示例
- Springboot注解之@EnableAutoConfiguration詳解
- SpringBoot中@EnableAutoConfiguration和@Configuration的區(qū)別
- SpringBoot中@EnableAutoConfiguration注解源碼分析
- SpringBoot使用@EnableAutoConfiguration實現(xiàn)自動配置詳解
- 解析SpringBoot @EnableAutoConfiguration的使用
- SpringBoot中@EnableAutoConfiguration注解的實現(xiàn)
相關(guān)文章
Java使用Lettuce客戶端在Redis在主從復制模式下命令執(zhí)行的操作
這篇文章主要介紹了Java使用Lettuce客戶端在Redis在主從復制模式下命令執(zhí)行的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-04-04Spring Boot 整合 ShedLock 處理定時任務重復執(zhí)行的問題小結(jié)
ShedLock是解決分布式系統(tǒng)中定時任務重復執(zhí)行問題的Java庫,通過在數(shù)據(jù)庫中加鎖,確保只有一個節(jié)點在指定時間執(zhí)行任務,它與SpringScheduler、Quartz等框架結(jié)合使用,本文介紹Spring Boot 整合 ShedLock 處理定時任務重復執(zhí)行的問題,感興趣的朋友一起看看吧2025-02-02詳解在Spring MVC中使用注解的方式校驗RequestParams
本篇文章主要介紹了詳解在Spring MVC中使用注解的方式校驗RequestParams ,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-03-03