Spring Boot--->執行原理初探
pom.xml
依賴一個父專案,管理專案資源,過濾外掛
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent>
父依賴
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.2.5.RELEASE</version> <relativePath>../../spring-boot-dependencies</relativePath> </parent>
這裡真正管理Spring Boot的所有依賴
以後的依賴就不需要寫版本了,但是匯入的包沒有在依賴中管理就需要手動配置版本了
springboot-boot-starter-xxx:就是spring-boot的場景啟動器
spring-boot-starter-web:幫我們匯入了web模組正常執行所依賴的元件;
預設的主配置類
@SpringBootApplication public class Springboot01Application { public static void main(String[] args) { SpringApplication.run(Springboot01Application.class, args); } }
一個簡單的配置類,@SpringBootApplication 這裡都做了寫什麼
@SpringBootConfiguration @EnableAutoConfiguration @ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class), @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) }) public @interface SpringBootApplication {
@ComponentScan
這個註解在Spring中很重要 ,它對應XML配置中的元素。
作用:自動掃描並載入符合條件的元件或者bean , 將這個bean定義載入到IOC容器中
@SpringBootConfiguration
作用:SpringBoot的配置類 ,標註在某個類上 , 表示這是一個SpringBoot的配置類;
我們繼續進去這個註解檢視
@Configuration public @interface SpringBootConfiguration {
這裡的 @Configuration,說明這是一個配置類 ,配置類就是對應Spring的xml 配置檔案;
@EnableAutoConfiguration
@EnableAutoConfiguration :開啟自動配置功能
以前我們需要自己配置的東西,而現在SpringBoot可以自動幫我們配置 ;@EnableAutoConfiguration告訴SpringBoot開啟自動配置功能,這樣自動配置才能生效;
@AutoConfigurationPackage :自動配置包
結論:
-
SpringBoot在啟動的時候從類路徑下的META-INF/spring.factories中獲取EnableAutoConfiguration指定的值
-
將這些值作為自動配置類匯入容器 , 自動配置類就生效 , 幫我們進行自動配置工作;
-
整個J2EE的整體解決方案和自動配置都在springboot-autoconfigure的jar包中;
-
它會給容器中匯入非常多的自動配置類 (xxxAutoConfiguration), 就是給容器中匯入這個場景需要的所有元件 , 並配置好這些元件 ;
-
有了自動配置類 , 免去了我們手動編寫配置注入功能元件等的工作;