自動裝配原理:
1):springboot啟動時加載主配置類,其中@EnableAutoConfiguration開啟自動裝配功能
2):@EnableAutoConfiguration作用:

? ? ? ? ? ? ? 1>:利用@Import導(dǎo)入了 AutoConfigurationImportSelector.class類,使用這個類給容器導(dǎo)入裝配的組件;
? ? ? ? ? ? ? 2>:其中的selectImports方法的內(nèi)容:
? ??????????????????????List configurations =this.getCandidateConfigurations(annotationMetadata, attributes);? ?獲取候選配置
? ? ? ? ? ? ? ? ? ? ? ??????????loadFactoryNames方法中掃描所有jar包的路徑

把掃描到的文件內(nèi)容包裝成propertier對象 --我們配置的配置文件內(nèi)容最后也會成功propertier對象
從properties中獲取到所有EnableAutoConfiguration.class類(對應(yīng)得值),然后把他們加載到容器中


將類路徑? :META-INF/spring.factories里面配置的所有EnableAutoConfiguration的值加入到容器中:
3):每一個自動配置類的功能
4):以HttpEncodingAutoConfiguration為例解釋自動配置原理:

? ? ? ? ? ? ? ? ? ? --@Configuration? ? :? 表示這是一個配置類,以前編寫的配置文件一個樣,給容器中添加組件
? ? ? ? ? ? ? ? ? ? -- @EnableConfigurationProperties? ?:啟動指定類的configurationProperties功能;將配置文件中對應(yīng)得值與ServerProperties綁定起來;
? ? ? ? ? ? ? ? ? ? --@ConditionalOnWebApplication? ?:? spring底層@Conditional注解,根據(jù)不同的條件,如果滿足指定的條件。整個配置類里面的配置生效;判斷應(yīng)用是否web應(yīng)用,如果是當(dāng)前配置類生效
? ? ? ? ? ? ? ? ? ? -- @ConditionalOnClass? ?: 判斷當(dāng)前項目有沒有這個類? ?CharacterEncodingFilter.class是springMvc中亂碼過濾器
? ? ? ? ? ? ? ? ? ? --@ConditionalOnProperty? ?:? 判斷配置文件中是否存在某個配置prefix ="server.servlet.encoding",,? ?不存在則enabled,默認(rèn)存在
? ? ? ? ? ? ? ? ? ? 太踏馬帥了~~~~~~~~~~

? ? ? ? ? ? ? ? ? ? --@Bean? ?:? 給容器添加一個組件? ?; 其中this.properies。get值表示某些之需要從properies文件中獲取
? ??????????????????????????????????private final Encoding properties;? 表示? this.properies已經(jīng)和properies文件映射了
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?重寫構(gòu)造方法,properies中參數(shù)的值從容器中獲取
? ??????????????????????????????????public HttpEncodingAutoConfiguration(ServerProperties properties) {
????????????????????????????????????????????????this.properties = properties.getServlet().getEncoding();
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?}
根據(jù)當(dāng)前不同的條件判斷,決定這個配置類是否生效?
5):配置文件中能配置的屬性都是對應(yīng)XXXXproperies的封裝著;配置文件能配置什么就可以參照某個功能對應(yīng)的這個屬性類
? ??????????????????
精髓:
? ? 1):SpringBoot啟動會加載大量的自動配置類
? ? ?2):我們看我們需要的功能有沒有SpringBoot默認(rèn)寫的自動配置類;
? ? ?3):我們在來看這個自動配置類中到底配置那些組件;(只要我們要用的組件有,我們就不要再來配置了
? ? ?4):給容器中自動配置類添加組件的時候,會從properties類中獲取某些屬性,我們就可以在配置文件中指定這些屬性的值
? ? ?xxxAutoConfiguration:自動配置類;? 給容器中添加組件
? ? ?xxxxProperties? :封裝配置文件中的相關(guān)屬性;
? ? ? ? ? ? ? ? ? ??