對上一篇SpringBoot中“HelloWorld”的探究
1.POM檔案
1.父專案
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent>
<!--上面的父專案為--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.2.1.RELEASE</version> <relativePath>../../spring-boot-dependencies</relativePath> </parent>
<!--這個才是真正管理SrpringBoot應用裡面的所有依賴版本-->
| 上即:SpringBoot的版本仲裁中心!
| 即此後匯入依賴預設是不需要寫版本;(沒有在dependencies裡面管理的依賴還是要寫的,但是在此包括幾乎所有要使用的依賴)
2.匯入的依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
| spring-boot-starter-web
|spring-boot-starter:spring-boot場景啟動器。即幫我們匯入了web模組正常執行所依賴的元件。
|starter:即一系列依賴大集合的描述。
SpringBoot將所有的功能場景都抽出來,做成一個個starters(啟動器),只需要在專案裡面引用這些starters相關場景,隨後其所需的所有依賴都會被導進來。並且版本由SpringBoot自動控制(即通過上面的父屬性)。要用什麼功能就匯入什麼場景的啟動器。
3.主程式類,主入口類
package com.test; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; //告訴springboot這是個springboot應用(@SpringBootApplication) //用於來標註主程式類 @SpringBootApplication public class HelloWorldMain { //args是可變引數 public static void main(String[] args) { /**將spring應用啟動起來 * HelloWorldMain.class 為主程式 **/ SpringApplication.run(HelloWorldMain.class,args); } }
|@SpringBootApplication:即SpringBoot應用,標註在某個類上,說明這個類是SpringBoot的主配置類。SpringBoot就應該執行這個類的main方法來啟動SpringBoot應用。
|SpringBootApplication:其本身為一個組合註解。
@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @SpringBootConfiguration @EnableAutoConfiguration @ComponentScan( excludeFilters = {@Filter( type = FilterType.CUSTOM, classes = {TypeExcludeFilter.class} ), @Filter( type = FilterType.CUSTOM, classes = {AutoConfigurationExcludeFilter.class} )} ) public @interface SpringBootApplication {
|@SpringBootConfiguration:SpringBoot的配置類;標註在某個類上,表示這是一個SpringBoot的配置類。
|@Configuration:配置類上標註這個註解。
| 上面所說的幾個配置類其實就相當於----平時寫的配置檔案;配置類也是容器中的元件。@Component
|@EnableAutoConfiguration:開啟自動配置功能。
@AutoConfigurationPackage @Import({AutoConfigurationImportSelector.class}) public @interface EnableAutoConfiguration {
|@AutoConfigurationPackage:自動配置包。
| @Import({AutoConfigurationPackage.Registrar.class}):即spring底層註解@Import,給容器匯入一個元件,匯入的元件由AutoConfigurationImportSelector.class,將主配置類(@SpringBootApplication所標註的類)的所在包以及下面所有子包裡面的所有元件掃描到Spring容器中去。
|@Import({AutoConfigurationImportSelector.class}):給容器中匯入元件。(該外掛翻譯為開啟自動配置類的導包的選擇器);AutoConfigurationImportSelector.class:即匯入哪些選擇器;將所有需要匯入的元件以全類名的方式返回。這些元件就會被新增到容器中去。 最終會給容器中匯入非常多的自動配置類(即xxxAutoConfiguration)。就是給容器中匯入這個場景所需要的所有元件,並且配置好這些元件。在此下圖有96個自動配置類。
| 有了這些自動配置類,就省的我們手動編寫配置注入等工作了。
|SpringFactoriesLoader.loadFactoryNames(@EnableAutoConfiguration.class,classLoader);
| SpringBoot在啟動的時候從類路徑下的META-INF/spring.factories中獲取EnableAutoConfiguration指定的值,將這些值作為自動配置類匯入到容器中,後,自動配置類生效,後,幫我們進行自動配置工作。
J2EE的整體整合方案和自動配置都在spring-boot-autoconfigure-2.2.1.RELEASE.jar裡面。
如有不妥,敬請斧正。