剛做后端開發(fā)的時(shí)候,最早接觸的是基礎(chǔ)的spring,為了引用二方包提供bean,還需要在xml中增加對(duì)應(yīng)的包<context:component-scan base-package="xxx" /> 或者增加注解@ComponentScan({ "xxx"})。當(dāng)時(shí)覺得挺urgly的,但也沒有去研究有沒有更好的方式。
在永福等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場(chǎng)前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都網(wǎng)站建設(shè)、做網(wǎng)站 網(wǎng)站設(shè)計(jì)制作按需求定制開發(fā),公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),成都品牌網(wǎng)站建設(shè),成都全網(wǎng)營銷,成都外貿(mào)網(wǎng)站建設(shè)公司,永福網(wǎng)站建設(shè)費(fèi)用合理。
直到接觸Spring Boot 后,發(fā)現(xiàn)其可以自動(dòng)引入二方包的bean。不過一直沒有看這塊的實(shí)現(xiàn)原理。直到最近面試的時(shí)候被問到。所以就看了下實(shí)現(xiàn)邏輯。
使用姿勢(shì)
講原理前先說下使用姿勢(shì)。
在project A中定義一個(gè)bean。
package com.wangzhi; import org.springframework.stereotype.Service; @Service public class Dog { }
并在該project的resources/META-INF/下創(chuàng)建一個(gè)叫spring.factories的文件,該文件內(nèi)容如下
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.wangzhi.Dog
然后在project B中引用project A的jar包。
projectA代碼如下:
package com.wangzhi.springbootdemo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.ComponentScan; @EnableAutoConfiguration public class SpringBootDemoApplication { public static void main(String[] args) { ConfigurableApplicationContext context = SpringApplication.run(SpringBootDemoApplication.class, args); System.out.println(context.getBean(com.wangzhi.Dog.class)); } }
打印結(jié)果:
com.wangzhi.Dog@3148f668
原理解析
總體分為兩個(gè)部分:一是收集所有spring.factories中EnableAutoConfiguration相關(guān)bean的類,二是將得到的類注冊(cè)到spring容器中。
收集bean定義類
在spring容器啟動(dòng)時(shí),會(huì)調(diào)用到AutoConfigurationImportSelector#getAutoConfigurationEntry
protected AutoConfigurationEntry getAutoConfigurationEntry( AutoConfigurationMetadata autoConfigurationMetadata, AnnotationMetadata annotationMetadata) { if (!isEnabled(annotationMetadata)) { return EMPTY_ENTRY; } // EnableAutoConfiguration注解的屬性:exclude,excludeName等 AnnotationAttributes attributes = getAttributes(annotationMetadata); // 得到所有的Configurations List<String> configurations = getCandidateConfigurations(annotationMetadata, attributes); // 去重 configurations = removeDuplicates(configurations); // 刪除掉exclude中指定的類 Set<String> exclusions = getExclusions(annotationMetadata, attributes); checkExcludedClasses(configurations, exclusions); configurations.removeAll(exclusions); configurations = filter(configurations, autoConfigurationMetadata); fireAutoConfigurationImportEvents(configurations, exclusions); return new AutoConfigurationEntry(configurations, exclusions); }
getCandidateConfigurations會(huì)調(diào)用到方法loadFactoryNames:
public static List<String> loadFactoryNames(Class<?> factoryClass, @Nullable ClassLoader classLoader) { // factoryClassName為org.springframework.boot.autoconfigure.EnableAutoConfiguration String factoryClassName = factoryClass.getName(); // 該方法返回的是所有spring.factories文件中key為org.springframework.boot.autoconfigure.EnableAutoConfiguration的類路徑 return loadSpringFactories(classLoader).getOrDefault(factoryClassName, Collections.emptyList()); } public static final String FACTORIES_RESOURCE_LOCATION = "META-INF/spring.factories"; private static Map<String, List<String>> loadSpringFactories(@Nullable ClassLoader classLoader) { MultiValueMap<String, String> result = cache.get(classLoader); if (result != null) { return result; } try { // 找到所有的"META-INF/spring.factories" Enumeration<URL> urls = (classLoader != null ? classLoader.getResources(FACTORIES_RESOURCE_LOCATION) : ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION)); result = new LinkedMultiValueMap<>(); while (urls.hasMoreElements()) { URL url = urls.nextElement(); UrlResource resource = new UrlResource(url); // 讀取文件內(nèi)容,properties類似于HashMap,包含了屬性的key和value Properties properties = PropertiesLoaderUtils.loadProperties(resource); for (Map.Entry<?, ?> entry : properties.entrySet()) { String factoryClassName = ((String) entry.getKey()).trim(); // 屬性文件中可以用','分割多個(gè)value for (String factoryName : StringUtils.commaDelimitedListToStringArray((String) entry.getValue())) { result.add(factoryClassName, factoryName.trim()); } } } cache.put(classLoader, result); return result; } catch (IOException ex) { throw new IllegalArgumentException("Unable to load factories from location [" + FACTORIES_RESOURCE_LOCATION + "]", ex); } }
注冊(cè)到容器
在上面的流程中得到了所有在spring.factories中指定的bean的類路徑,在processGroupImports方法中會(huì)以處理@import注解一樣的邏輯將其導(dǎo)入進(jìn)容器。
public void processGroupImports() { for (DeferredImportSelectorGrouping grouping : this.groupings.values()) { // getImports即上面得到的所有類路徑的封裝 grouping.getImports().forEach(entry -> { ConfigurationClass configurationClass = this.configurationClasses.get( entry.getMetadata()); try { // 和處理@Import注解一樣 processImports(configurationClass, asSourceClass(configurationClass), asSourceClasses(entry.getImportClassName()), false); } catch (BeanDefinitionStoreException ex) { throw ex; } catch (Throwable ex) { throw new BeanDefinitionStoreException( "Failed to process import candidates for configuration class [" + configurationClass.getMetadata().getClassName() + "]", ex); } }); } } private void processImports(ConfigurationClass configClass, SourceClass currentSourceClass, Collection<SourceClass> importCandidates, boolean checkForCircularImports) { ... // 遍歷收集到的類路徑 for (SourceClass candidate : importCandidates) { ... //如果candidate是ImportSelector或ImportBeanDefinitionRegistrar類型其處理邏輯會(huì)不一樣,這里不關(guān)注 // Candidate class not an ImportSelector or ImportBeanDefinitionRegistrar -> // process it as an @Configuration class this.importStack.registerImport( currentSourceClass.getMetadata(), candidate.getMetadata().getClassName()); // 當(dāng)作 @Configuration 處理 processConfigurationClass(candidate.asConfigClass(configClass)); ... } ... }
可以看到,在第一步收集的bean類定義,最終會(huì)被以Configuration一樣的處理方式注冊(cè)到容器中。
End
@EnableAutoConfiguration注解簡(jiǎn)化了導(dǎo)入了二方包bean的成本。提供一個(gè)二方包給其他應(yīng)用使用,只需要在二方包里將對(duì)外暴露的bean定義在spring.factories中就好了。對(duì)于不需要的bean,可以在使用方用@EnableAutoConfiguration的exclude屬性進(jìn)行排除。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
本文名稱:解析SpringBoot@EnableAutoConfiguration的使用
本文來源:http://jinyejixie.com/article46/iicseg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動(dòng)態(tài)網(wǎng)站、移動(dòng)網(wǎng)站建設(shè)、軟件開發(fā)、微信小程序、網(wǎng)站導(dǎo)航、定制網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)