自動裝配:
pom.xml
- spring-boot-dependence:核心都依賴在父類工程中!
- 我們在寫入或者引入springboot依賴的時候,不需要指定版,因為有這些倉庫的版本
啟動器:------spring boot的啟動場景
- 比如spring-boot-starter-web,他就會幫我們匯入web環境蘇需要的依賴,
- springboot會將所有的功能場景,都變成一個個啟動器,
- 我們使用什么功能,只需要找到對應的啟動器(starter)就可以了
主程式:
//springbootApplication:標注這個類是一個springboot的應用,啟動類下的所有資源被匯入
@SpringBootApplication
public class HuangApplication {
public static void main(String[] args) {
SpringApplication.run(HuangApplication.class, args);
}
}
注解:
@SpringBootConfiguration:springboot的配置
@Configuration: sring配置類
@@Component:說明這也是一個spring的組件
@@EnableAutoConfiguration: 自動配置
@AutoConfigurationPackage:自動配置
@Import(AutoConfigurationPackages.Registrar.class):匯入選擇器
@Import({AutoConfigurationImportSelector.class}):自動匯入選擇
//獲取所有的配置
List<String>configurations=this.getCandidateConfigurations(annotationMetadata, attributes);
獲取候選的配置:
protectedList<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
List<String> configurations = new ArrayList(SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.getBeanClassLoader()));
ImportCandidates.load(AutoConfiguration.class, this.getBeanClassLoader()).forEach(configurations::add);
Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories nor in META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports. If you are using a custom packaging, make sure that file is correct.");
return configurations;
自動配置的核心:
META-INF/spring.factories
Properties properties = PropertiessloaderUtils.loadProperties(resource);
所有資源加載到配置類中!
結論:
? springboot所有的自動配置都是在啟動的時候掃描并加載:spring.factoriess所有的自動配置類都在這里面,但不一定全部啟動,要判斷條件是否成立,只要匯入對應的start,就有對應的啟動器了,有了啟動器,我們的自動裝配就會生效,然后配置就會成功,
大概步驟:
1.
spring boot在啟動的時候,從路徑下/META-INF/spring.factories獲取指定的值,
2.
將這些自動配置的類匯入容器,自動配置就會生效,進行自動配置,
3.
以前我們需要自動配置的東西,現在都在springboot幫我們做,
4.
整合javaEE,解決方案和自動裝配的東西都在spring-boot-autofigure- 2.7.11.RELEASE.JAR這個包下面
5.
他會把所有需要匯入的組件,以類的方式回傳,這些組件就會被添加到容器,
6.
容器中也會存在非常多的xxxAutoConfiguration的檔案(@bean),就是這些類給容器中匯入了這個場景需要的所有組件,并自動配置,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/551176.html
標籤:其他
下一篇:返回列表
