1.源碼剖析-依賴管理
(1)為什么導(dǎo)入dependency時不需要指定版本懒震?
在Spring Boot入門程序中罩息,項目pom.xml文件有兩個核心依賴,分別是spring-boot-starter?parent和spring-boot-starter-web个扰,關(guān)于這兩個依賴的相關(guān)介紹具體如下:
1.1.spring-boot-starter-parent依賴
pom.xml文件中找到spring-boot-starter-parent依賴瓷炮,示例代碼如下:
<!-- Spring Boot父項目依賴管理 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent<11./artifactId>
<version>2.2.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository -->
</parent>
將spring-boot-starter-parent依賴作為Spring Boot項目的統(tǒng)一父項目依賴管理,并將項目版本號統(tǒng)一為2.2.2.RELEASE递宅,該版本號根據(jù)實際開發(fā)需求是可以修改的.
?? 看spring-boot-starter-parent底層源文件娘香,發(fā)現(xiàn)spring-boot?starter-parent的底層有一個父依賴spring-boot-dependencies,核心代碼具體如下:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath>../../spring-boot-dependencies</relativePath>
</parent>
而看spring-boot-dependencies底層源文件办龄,核心代碼具體如下:
<properties>
<activemq.version>5.15.11</activemq.version>
...
<solr.version>8.2.0</solr.version>
<mysql.version>8.0.18</mysql.version>
<kafka.version>2.3.1</kafka.version>
<spring-amqp.version>2.2.2.RELEASE</spring-amqp.version>
<spring-restdocs.version>2.0.4.RELEASE</spring-restdocs.version>
<spring-retry.version>1.2.4.RELEASE</spring-retry.version>
<spring-security.version>5.2.1.RELEASE</spring-security.version>
<spring-session-bom.version>Corn-RELEASE</spring-session-bom.version>
<spring-ws.version>3.0.8.RELEASE</spring-ws.version>
<sqlite-jdbc.version>3.28.0</sqlite-jdbc.version>
<sun-mail.version>${jakarta-mail.version}</sun-mail.version>
<tomcat.version>9.0.29</tomcat.version>
<thymeleaf.version>3.0.11.RELEASE</thymeleaf.version>
<thymeleaf-extras-data-attribute.version>2.0.1</thymeleaf-extras-data- attribute.version>
...
</properties>
該文件通過標(biāo)簽對一些常用技術(shù)框架的依賴文件進(jìn)行了統(tǒng)一版本號管理烘绽,都有與Spring Boot 2.2.2版本相匹配的版本,這也是pom.xml引入依賴文件不需要標(biāo)注依賴文件版本號的原因俐填。
??如果pom.xml引入的依賴文件不是 spring-boot-starter-parent管理的安接,那么在
pom.xml引入依賴文件時,需要使用標(biāo)簽指定依賴文件的版本號英融。
(2) spring-boot-starter-parent父依賴啟動器的主要作用是進(jìn)行版本統(tǒng)一管理盏檐,那么項目運
行依賴的JAR包是從何而來的?
1.2 spring-boot-starter-web依賴
源碼如下
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.2.2.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-json</artifactId>
<version>2.2.2.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>2.2.2.RELEASE</version> <scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<version>2.2.2.RELEASE</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<artifactId>tomcat-embed-el</artifactId>
<groupId>org.apache.tomcat.embed</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.2.2.RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.2.RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
*spring-boot-starter-web依賴啟動器的主要作用是提供Web開發(fā)場景所需的底層所有依賴;
*Spring Boot除了提供有上述介紹的Web依賴啟動器外驶悟,還提供了其他許多開發(fā)場景的相關(guān)依賴
2. 自動配置(啟動流程)
總結(jié)
因此springboot底層實現(xiàn)自動配置的步驟是:
(1). springboot應(yīng)用啟動糯笙;
??啟動入口是@SpringBootApplication注解標(biāo)注類中的main()方法, @SpringBootApplication能夠掃描Spring組件并自動配置Spring Boot
(2). @SpringBootApplication起作用撩银;
@SpringBootApplication注解是一個組合注解,前面 4 個是注解的元數(shù)據(jù)信息豺憔, 我們主要看后面 3 個注解:@SpringBootConfiguration额获、@EnableAutoConfiguration、@ComponentScan三個核心注解.
*@SpringBootConfiguration
作用與@Configuration注解相同恭应,都是標(biāo)識一個可以被組件掃描器掃描的配置類抄邀,只不過@SpringBootConfiguration是被Spring Boot進(jìn)行了重新封裝命名而已
(3). @EnableAutoConfiguration;
@EnableAutoConfiguration注解表示開啟自動配置功能昼榛,該注解是Spring Boot框架最重要的注
解境肾,也是實現(xiàn)自動化配置的注解。他是由@AutoConfigurationPackage注解和@Import({AutoConfigurationImportSelector.class})組合而成;
(4). @AutoConfigurationPackage:
這個組合注解主要是@Import(AutoConfigurationPackages.Registrar.class),它通過將Registrar類導(dǎo)入到容器中奥喻,而Registrar類作用是掃描主配置類同級目錄以及子包偶宫,并將相應(yīng)的組件導(dǎo)入到springboot創(chuàng)建管理
的容器中;
(5). @Import(AutoConfigurationImportSelector.class):它通過AutoConfigurationImportSelector類導(dǎo)入到容器中环鲤,AutoConfigurationImportSelector類作用是通過selectImports方法執(zhí)行的過程中纯趋,會使用內(nèi)部工具類SpringFactoriesLoader,查找classpath上所有jar包中METAINF/spring.factories進(jìn)行加載冷离,實現(xiàn)將配置類信息交給SpringFactory加載器進(jìn)行一系列的容器創(chuàng)建過程