最新springboot中必須要了解的自動(dòng)裝配原理
1.pom.xml
父 依 賴 \textcolor{orange}{父依賴} 父依賴
spring-boot-dependencies:核心依賴都在父工程中
這里ctrl+左鍵,點(diǎn)擊之后我們可以看到父依賴
這個(gè)里面主要是管理項(xiàng)目的資源過濾及插件,我們發(fā)現(xiàn)他還有一個(gè)父依賴
看看下面這個(gè),熟悉嗎?
再點(diǎn)進(jìn)去,我們發(fā)現(xiàn)有很多的依賴。這就是SpringBoot的版本控制中心。
這個(gè)地方才是真正管理SpringBoot應(yīng)用里面所有依賴的地方,也就是版本控制中心。
我們?cè)趯懟蛞胍恍㏒pringBoot依賴的時(shí)候,不需要指定版本,就是因?yàn)橛羞@些版本倉庫。
2.啟動(dòng)器
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
啟動(dòng)器就是springboot的啟動(dòng)場(chǎng)景 \textcolor{red}{啟動(dòng)器就是SpringBoot的啟動(dòng)場(chǎng)景} 啟動(dòng)器就是SpringBoot的啟動(dòng)場(chǎng)景
springboot-boot-starter-xxx:就是spring-boot的場(chǎng)景啟動(dòng)器
spring-boot-starter-web:幫我們導(dǎo)入了web模塊正常運(yùn)行所依賴的組件;也就是自動(dòng)導(dǎo)入web環(huán)境所有的依賴
SpringBoot將所有的功能場(chǎng)景都抽取出來,做成一個(gè)個(gè)的starter (啟動(dòng)器);
要用什么功能就導(dǎo)入什么樣的場(chǎng)景啟動(dòng)器:只需要在項(xiàng)目中引入這些starter
即可,所有相關(guān)的依賴都會(huì)導(dǎo)入進(jìn)來 ;
我們未來也可以自己自定義 starter;
3.主程序
//程序的主入口 //@SpringBootApplication:標(biāo)注這個(gè)類是一個(gè)springBoot的應(yīng)用 @SpringBootApplication public class HelloSpringBootApplication { public static void main(String[] args) { //將springBoot應(yīng)用啟動(dòng) SpringApplication.run(HelloSpringBootApplication.class, args); } }
看著如此的簡(jiǎn)單,它就是通過反射加載這個(gè)類的對(duì)象,這是表面意思,我們看不到它為啥啟動(dòng)。
首先我們來看
3.1注解
@SpringBootApplication
我們點(diǎn)擊@SpringBootApplication
后可以看到有這么幾個(gè)注解
結(jié)論:springBoot所有的自動(dòng)配置都是在啟動(dòng)的時(shí)候掃描并加載:spring.factories
所有的自動(dòng)配置類都在這里面,但是不一定生效,要判斷條件是否成立,只要導(dǎo)入了對(duì)應(yīng)的start,就有了對(duì)應(yīng)的啟動(dòng)器,有了啟動(dòng)器,自動(dòng)裝配就是生效,之后配置成功
@ComponentScan
這個(gè)注解在Spring中非常重要,對(duì)應(yīng)的是XML配置中的元素。
作用:自動(dòng)掃描并加載符合條件的組件或者bean,將這個(gè)bean定義加載到IOC容器中
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class), @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
@SpringBootConfiguration
作用:SpringBoot的配置類 ,標(biāo)注在某個(gè)類上 , 表示這是一個(gè)SpringBoot的配置類;
@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Configuration @Indexed public @interface SpringBootConfiguration { @AliasFor( annotation = Configuration.class ) boolean proxyBeanMethods() default true; }
這里的@Configuration
說明這是一個(gè)配置類,這個(gè)配置類就是對(duì)應(yīng)Spring的xml配置文件
@Component
說明,啟動(dòng)類本身也是Spring中的一個(gè)組件,負(fù)責(zé)啟動(dòng)應(yīng)用。
@EnableAutoConfiguration
作用:開啟自動(dòng)配置功能
點(diǎn)進(jìn)去后會(huì)看到
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @AutoConfigurationPackage @Import(AutoConfigurationImportSelector.class)
然后我們發(fā)現(xiàn)了@AutoConfigurationPackage
它的作用是自動(dòng)配置包
@Import(AutoConfigurationPackages.Registrar.class) public @interface AutoConfigurationPackage { }
@import
:Spring底層注解@import , 給容器中導(dǎo)入一個(gè)組件
AutoConfigurationPackages.Registrar.class
作用:將主啟動(dòng)類的所在包及包下面所有子包里面的所有組件掃描到Spring容器 ;
我們退回上一步看一下這個(gè)注解
@Import({AutoConfigurationImportSelector.class})
:給容器導(dǎo)入組件 ;
AutoConfigurationImportSelector :自動(dòng)配置導(dǎo)入選擇器,在這個(gè)類中有這么一個(gè)方法
/** * Return the auto-configuration class names that should be considered. By default * this method will load candidates using {@link SpringFactoriesLoader} with * {@link #getSpringFactoriesLoaderFactoryClass()}. * @param metadata the source metadata * @param attributes the {@link #getAttributes(AnnotationMetadata) annotation * attributes} * @return a list of candidate configurations */ //獲得候選的配置 protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) { //這里的getSpringFactoriesLoaderFactoryClass() //返回的是我們最開是看到啟動(dòng)自動(dòng)導(dǎo)入配置文件的注解類;EnableAutoConfiguration 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; }
在上面這個(gè)方法中調(diào)用了SpringFactoriesLoader
這個(gè)類中的靜態(tài)方法,我們查看一下這個(gè)類中的loadFactoryNames
這個(gè)方法。
public static List<String> loadFactoryNames(Class<?> factoryType, @Nullable ClassLoader classLoader) { ClassLoader classLoaderToUse = classLoader; if (classLoader == null) { classLoaderToUse = SpringFactoriesLoader.class.getClassLoader(); } String factoryTypeName = factoryType.getName(); return (List)loadSpringFactories(classLoaderToUse).getOrDefault(factoryTypeName, Collections.emptyList()); }
發(fā)現(xiàn)他又調(diào)用了loadSpringFactories
這個(gè)方法,我們繼續(xù)看
private static Map<String, List<String>> loadSpringFactories(ClassLoader classLoader) { Map<String, List<String>> result = (Map)cache.get(classLoader); if (result != null) { return result; } else { HashMap result = new HashMap(); try { Enumeration urls = classLoader.getResources("META-INF/spring.factories"); while(urls.hasMoreElements()) { URL url = (URL)urls.nextElement(); UrlResource resource = new UrlResource(url); Properties properties = PropertiesLoaderUtils.loadProperties(resource); Iterator var6 = properties.entrySet().iterator(); while(var6.hasNext()) { Entry<?, ?> entry = (Entry)var6.next(); String factoryTypeName = ((String)entry.getKey()).trim(); String[] factoryImplementationNames = StringUtils.commaDelimitedListToStringArray((String)entry.getValue()); String[] var10 = factoryImplementationNames; int var11 = factoryImplementationNames.length; for(int var12 = 0; var12 < var11; ++var12) { String factoryImplementationName = var10[var12]; ((List)result.computeIfAbsent(factoryTypeName, (key) -> { return new ArrayList(); })).add(factoryImplementationName.trim()); } } } result.replaceAll((factoryType, implementations) -> { return (List)implementations.stream().distinct().collect(Collectors.collectingAndThen(Collectors.toList(), Collections::unmodifiableList)); }); cache.put(classLoader, result); return result; } catch (IOException var14) { throw new IllegalArgumentException("Unable to load factories from location [META-INF/spring.factories]", var14); } } }
這里我們發(fā)現(xiàn)多次出現(xiàn)了一個(gè)叫spring.factories\textcolor{red}{這里我們發(fā)現(xiàn)多次出現(xiàn)了一個(gè)叫spring.factories} 這里我們發(fā)現(xiàn)多次出現(xiàn)了一個(gè)叫spring.factories
3.2 spring.factories
隨便點(diǎn)一個(gè)看看JerseyAutoConfiguration
會(huì)發(fā)現(xiàn)這都是javaConfig配置類,而且都注入了一些Bean。
所以,自動(dòng)配置真正實(shí)現(xiàn)是從classpath中搜尋所有的META-INF/spring.factories配置文件 ,并將其中對(duì)應(yīng)的 org.springframework.boot.autoconfigure. 包下的配置項(xiàng),通過反射實(shí)例化為對(duì)應(yīng)標(biāo)注了 @Configuration的JavaConfig形式的IOC容器配置類 , 然后將這些都匯總成為一個(gè)實(shí)例并加載到IOC容器中。
4. 結(jié)論
- springboot在啟動(dòng)的時(shí)候,從類路徑下
/META-INF/spring.factories
獲取指定的值; - 將這些自動(dòng)配置的類導(dǎo)入容器,自動(dòng)配置就會(huì)生效,進(jìn)行自動(dòng)配置;
- 以前需要自動(dòng)配置的東西,現(xiàn)在
springboot
幫忙做了; - 整合JavaEE,解決方案和自動(dòng)配置的東西都在
spring-boot-autoconfigure-2.5.7.jar
這個(gè)包下 - 他會(huì)把所有需要導(dǎo)入的組件,以類名的方式返回,這些組件就會(huì)被添加到容器中
- 容器中也會(huì)存在非常多的xxxAutoConfiguration的文件(@Bean),就是這些類給容器中的導(dǎo)入這個(gè)場(chǎng)景需要的所有組件,并自動(dòng)配置。@Configuration,javaCOnfig
- 有了自動(dòng)配置類,免去了我們手動(dòng)編寫配置文件的工作。
到此這篇關(guān)于最新springboot中必須要了解的自動(dòng)裝配原理的文章就介紹到這了,更多相關(guān)springboot自動(dòng)裝配原理內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot?整合mapstruct的實(shí)現(xiàn)步驟
這篇文章主要介紹了SpringBoot整合mapstruct,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11關(guān)于idea-web.xml版本過低怎么生成新的(web.xml報(bào)錯(cuò))問題
今天通過本文給大家分享idea-web.xml版本過低怎么生成新的(web.xml報(bào)錯(cuò))問題,通過更換web.xml版本解決此問題,感興趣的朋友跟隨小編一起看看吧2021-07-07SpringBoot 3.0 新特性內(nèi)置聲明式HTTP客戶端實(shí)例詳解
聲明式 http 客戶端主旨是使得編寫 java http 客戶端更容易,為了貫徹這個(gè)理念,采用了通過處理注解來自動(dòng)生成請(qǐng)求的方式,本文給大家詳解介紹SpringBoot 聲明式HTTP客戶端相關(guān)知識(shí),感興趣的朋友跟隨小編一起看看吧2022-12-12JavaWeb實(shí)現(xiàn)多文件上傳及zip打包下載
這篇文章主要為大家詳細(xì)介紹了JavaWeb實(shí)現(xiàn)多文件上傳及zip打包下載,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07Eclipse配置tomcat發(fā)布路徑的問題wtpwebapps解決辦法
這篇文章主要介紹了Eclipse配置tomcat發(fā)布路徑的問題wtpwebapps解決辦法的相關(guān)資料,需要的朋友可以參考下2017-06-06詳解Spring AOP 攔截器的基本實(shí)現(xiàn)
本篇文章主要介紹了詳解Spring AOP 攔截器的基本實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-03-03解決Spring Cloud Feign 請(qǐng)求時(shí)附帶請(qǐng)求頭的問題
這篇文章主要介紹了解決Spring Cloud Feign 請(qǐng)求時(shí)附帶請(qǐng)求頭的問題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10