springboot自動裝配的源碼與流程圖
前言
在使用SpringBoot開發(fā)項目中,遇到一些 XXX-XXX-starter,例如mybatis-plus-boot-starter,這些包總是能夠自動進(jìn)行配置,
減少了開發(fā)人員配置一些項目配置的時間,讓開發(fā)者擁有更多的時間用于開發(fā)的任務(wù)上面。下面從源碼開始。
正文
SpringBoot版本:2.5.3
- 從@SpringBootApplication進(jìn)入@EnableAutoConfiguration
- 然后進(jìn)入AutoConfigurationImportSelector
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @SpringBootConfiguration @EnableAutoConfiguration @ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class), @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) }) public @interface SpringBootApplication {} @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @AutoConfigurationPackage @Import(AutoConfigurationImportSelector.class) public @interface EnableAutoConfiguration {}
進(jìn)入AutoConfigurationImportSelector,可以發(fā)現(xiàn)該類是ImportSelector接口的實現(xiàn)類,然后直接定位至selectImports方法。
到了這里,其實主動裝配的套路就是@EnableXXX加@Import的套路。這就是一個大概的認(rèn)知了。
@Override public String[] selectImports(AnnotationMetadata annotationMetadata) { if (!isEnabled(annotationMetadata)) { return NO_IMPORTS; } AutoConfigurationEntry autoConfigurationEntry = getAutoConfigurationEntry(annotationMetadata); return StringUtils.toStringArray(autoConfigurationEntry.getConfigurations()); }
下面進(jìn)入getAutoConfigurationEntry方法:
protected AutoConfigurationEntry getAutoConfigurationEntry(AnnotationMetadata annotationMetadata) { if (!isEnabled(annotationMetadata)) { return EMPTY_ENTRY; } // 獲取注解信息 AnnotationAttributes attributes = getAttributes(annotationMetadata); // 獲取自動配置的信息 List<String> configurations = getCandidateConfigurations(annotationMetadata, attributes); // 去重 configurations = removeDuplicates(configurations); // 獲取需要去除的信息 Set<String> exclusions = getExclusions(annotationMetadata, attributes); // 檢查 checkExcludedClasses(configurations, exclusions); // 去除需要被去除的配置 configurations.removeAll(exclusions); configurations = getConfigurationClassFilter().filter(configurations); fireAutoConfigurationImportEvents(configurations, exclusions); return new AutoConfigurationEntry(configurations, exclusions); }
詳細(xì)的注釋已經(jīng)寫在代碼中的了,這里面最重要的是getCandidateConfigurations方法,其次是下面的過濾排除不需要的配置信息。下面進(jìn)入getCandidateConfigurations方法。
protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) { // 重要 List<String> configurations = SpringFactoriesLoader.loadFactoryNames(getSpringFactoriesLoaderFactoryClass(), getBeanClassLoader()); Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories. If you " + "are using a custom packaging, make sure that file is correct."); return configurations; } protected Class<?> getSpringFactoriesLoaderFactoryClass() { return EnableAutoConfiguration.class; } protected ClassLoader getBeanClassLoader() { return this.beanClassLoader; }
這里需要關(guān)注的方法是SpringFactoriesLoader.loadFactoryNames,進(jìn)入該方法,該方法是一個靜態(tài)方法。
public static List<String> loadFactoryNames(Class<?> factoryType, @Nullable ClassLoader classLoader) { ClassLoader classLoaderToUse = classLoader; if (classLoaderToUse == null) { classLoaderToUse = SpringFactoriesLoader.class.getClassLoader(); } // 獲取類名 String factoryTypeName = factoryType.getName(); return loadSpringFactories(classLoaderToUse).getOrDefault(factoryTypeName, Collections.emptyList()); } private static Map<String, List<String>> loadSpringFactories(ClassLoader classLoader) { // 查詢緩存 Map<String, List<String>> result = cache.get(classLoader); if (result != null) { return result; } result = new HashMap<>(); try { // 1. 獲取類加載器能讀取的所有在META-INF目錄下的spring.factories文件 Enumeration<URL> urls = classLoader.getResources(FACTORIES_RESOURCE_LOCATION); while (urls.hasMoreElements()) { // 遍歷路徑 URL url = urls.nextElement(); UrlResource resource = new UrlResource(url); // 將路徑下的文件數(shù)據(jù)讀取為Properties Properties properties = PropertiesLoaderUtils.loadProperties(resource); // 遍歷Properties for (Map.Entry<?, ?> entry : properties.entrySet()) { String factoryTypeName = ((String) entry.getKey()).trim(); String[] factoryImplementationNames = StringUtils.commaDelimitedListToStringArray((String) entry.getValue()); for (String factoryImplementationName : factoryImplementationNames) { result.computeIfAbsent(factoryTypeName, key -> new ArrayList<>()) .add(factoryImplementationName.trim()); } } } // Replace all lists with unmodifiable lists containing unique elements // 用不可修改列表替換 result.replaceAll((factoryType, implementations) -> implementations.stream().distinct() .collect(Collectors.collectingAndThen(Collectors.toList(), Collections::unmodifiableList))); // 加入緩存 cache.put(classLoader, result); } catch (IOException ex) { throw new IllegalArgumentException("Unable to load factories from location [" + FACTORIES_RESOURCE_LOCATION + "]", ex); } return result; }
- loadSpringFactories方法就是加載并讀取所傳入的類加載器能讀取的所有spring.factories文件,并將讀取的數(shù)據(jù)最終轉(zhuǎn)換為Map<String, List>類型的數(shù)據(jù),然后存入本地緩存。
- loadFactoryNames方法就是通過傳入factoryType(也就是calss.name)來獲取數(shù)據(jù),并不是獲取所有的數(shù)據(jù)。所以這里會有很多地方會用到。
- @EnableAutoConfiguration是取的文件中的org.springframework.boot.autoconfigure.EnableAutoConfiguration,所以一般自定義starter的自動配置文件都是在這個key后面。例如:org.springframework.boot.autoconfigure.EnableAutoConfiguration= a.b.c.d.XXX;
至此,源碼就差不多完成了,其實從源碼上來看其實也不難。
大概流程圖如下:
最后
說了這么多源碼,也畫了流程圖。這里面最重要的就是SpringFactoriesLoader,從這個類名就可以看出來,是專門處理factories文件的,這個類只提供了兩個靜態(tài)方法,而EnableAutoConfiguration只是取了其中的EnableAutoConfiguration下的數(shù)據(jù),那么其它的數(shù)據(jù)呢,不會用到嗎?肯定不會的,所以spring在很多地方會用到這個類,后面看spring源碼的時候在來看吧,這里先標(biāo)記一下。
還有就是,SpringFactoriesLoader和JDK的SPI也是差不多的一個思想。下一篇就來看看JDK的SPI
到此這篇關(guān)于springboot自動裝配的源碼與流程圖的文章就介紹到這了,更多相關(guān)springboot自動裝配內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring Boot 配置MySQL數(shù)據(jù)庫重連的操作方法
這篇文章主要介紹了Spring Boot 配置MySQL數(shù)據(jù)庫重連的操作方法,需要的朋友可以參考下2018-04-04SpringBoot導(dǎo)入導(dǎo)出數(shù)據(jù)實現(xiàn)方法詳解
這篇文章主要介紹了SpringBoot導(dǎo)入導(dǎo)出數(shù)據(jù)實現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-12-12SpringBoot實現(xiàn)前端驗證碼圖片生成和校驗
這篇文章主要為大家詳細(xì)介紹了SpringBoot實現(xiàn)前端驗證碼圖片生成和校驗,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-02-02mybatis多個區(qū)間處理方式(雙foreach循環(huán))
這篇文章主要介紹了mybatis多個區(qū)間處理方式(雙foreach循環(huán)),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02