一、前言
Springboot的自動配置原理,面試中經常問到,一直看也記不住,不如手寫一個starter,加深一下記憶,
看了之后發現大部分的starter都是這個原理,實踐才會記憶深刻,
核心思想:約定大于配置,
二、初探starter啟動原理
我們直接看看官網的starter是怎么設計的,仿照這寫一個就行了!
我們Ctrl點擊<artifactId>spring-boot-starter-web</artifactId>,進入內部pom,我們發現里面有個
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.5.3</version>
<scope>compile</scope>
</dependency>
在此Ctrl點擊<artifactId>spring-boot-starter</artifactId>進入starter內部pom:
我們發現之后干活的就是這個包依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.5.3</version>
<scope>compile</scope>
</dependency>

我們現在創建兩個專案即可:
- spring-boot-starter(啟動器)
- spring-boot-starter-autoconfigure(自動配置包)
小編看到官方這么寫的提醒,大家可以按照官網的進行起名稱,不要學小編哈!!
您應該確保為您的啟動器提供適當的命名空間,不要以 . 開頭的模塊名稱spring-boot,即使您使用不同的 Maven groupId,我們將來可能會為您自動配置的內容提供官方支持,
根據經驗,您應該在啟動器之后命名組合模塊,例如,假設您正在為“acme”創建一個啟動器,并且您命名自動配置模塊acme-spring-boot和啟動器acme-spring-boot-starter,如果您只有一個模塊將兩者結合起來,請將其命名為acme-spring-boot-starter.
三、專案搭建
1. 新建空白專案

輸入總的專案名稱

在空白專案里新建兩個,這里可以分開單獨建立,這里小編跟著雷神一樣了,就不單獨建立專案了!!

2. 新建maven專案

包名和名稱:

3. 新建springboot專案

4. 專案架構
這里把沒有用的都洗掉了!!可以不刪

四、配置
1. 在starter專案中引入自己的autoconfigure依賴
就是上面建立專案的設定的
<dependencies>
<dependency>
<groupId>com.wang</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>

2. spring-boot-autoconfigure pom配置
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.3.7.RELEASE</spring-boot.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
</dependency>
</dependencies>
還是把沒用的東西洗掉了,剩余如下圖:

五、撰寫autoconfigure專案
1. 配置一個bean
首先洗掉了自動啟動類,咱們用不到,
在spring-boot-autoconfigure專案中新建一個bean,此時不需要讓這個bean在容器中,我們寫一個自己配置,讓他自動加入到容器中,
這就是自動配置的思想
/**
* 這里不需要讓在容器中,我們寫一個自己配置,讓他自動加入到容器中
* @author wangzhenjun
* @date 2022/10/14 16:26
*/
public class HelloService {
@Autowired
private MyProperties myProperties;
public String HelloWord (String username){
return myProperties.getPrefix() + username + myProperties.getSuffix();
}
}
2. 撰寫一個組態檔
這里為了獲取組態檔中的屬性值,springboot自動配置原始碼里大部分都是,這樣實作在一個組態檔中書寫,其他的可以按照開頭獲取到屬性和值!!
/**
* @author wangzhenjun
* @date 2022/10/14 16:28
*/
@Data
@ConfigurationProperties("wang.name")
public class MyProperties {
private String prefix;
private String suffix;
}
3. 撰寫自動配置
import com.wang.springbootautoconfigure.properties.MyProperties;
import com.wang.springbootautoconfigure.service.HelloService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author wangzhenjun
* @date 2022/10/14 16:33
*/
@Configuration
//沒有這個bean的時候才會生效
@ConditionalOnMissingBean(HelloService.class)
// 加載組態檔,讓它成為容器中的bean
@EnableConfigurationProperties(MyProperties.class)
public class ServiceAutoConfiguration {
/**
* 把剛剛寫的服務,加入到容器中
*/
@Bean
public HelloService helloService (){
return new HelloService();
}
}
主要就是condition下的幾個注解,來完成bean是否加入到容器中:
常用的:
- @ConditionalOnClass
- @ConditionalOnMissingClass
- @ConditionalOnBean
- @ConditionalOnMissingBean
- @ConditionalOnProperty

4. 新建spring.factories
我們看到原始碼里自動配置,就是從這個檔案獲取加載,所以我們模仿這新建一個,這樣就可以掃描加入容器中!!

如果是springboot2.7以上就是:
檔案夾名稱:META-INF.spring
檔案名稱:org.springframework.boot.autoconfigure.AutoConfiguration.imports

里面直接寫全類名即可!
5. 打包
先把spring-boot-autoconfigure打包到本地庫,在打包spring-boot-starter,順序一定要有,不然找不到前者的包!!

六、測驗
我們那一個新專案進行測驗,新專案小編就不帶大家建了!
1. 匯入咱們的starter依賴
<dependency>
<groupId>com.wang</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
2. 添加組態檔
wang:
name:
prefix: hello
suffix: 886

3. 新建controller測驗類
@RestController
@RequestMapping("/test")
public class TestController {
@Autowired
private HelloService helloService;
@GetMapping("/starter")
public String starter(){
return helloService.HelloWord("tom");
}
}
4. 測驗訪問
成功訪問,不過中文是有亂碼的,小編找不到解決方法,有懂的還請留言告知!!
主流程通了就行,慢慢理解了自動配置的魅力!
總流程應該就是這樣的:
引入starter --- xxxAutoConfiguration --- 容器中放入組件 ---- 系結xxxProperties ---- 配置項

中文是亂碼,可能是servlet沒有吧,有懂的可以留言告訴小編方案,謝謝大家!!

七、總結
一看會就,一動手就廢!大家還是要做自己實操,不要眼高手低,這樣才會有識訓,根本就是約定大于配置+SPI發現機制!
還有就是一些經常出現在原始碼里的注解,大家記住就可以自己寫starter了!
可以看下一小編的微信公眾號,和網站文章首發看,歡迎關注!!

點擊訪問!小編自己的網站,里面也是有很多好的文章哦!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/518697.html
標籤:Java
