SpringBoot自定義Starter與自動配置實現(xiàn)方法詳解
前言
前段時間,SpringBoot 出 3.x 版本了。聽說把自動配置給刀了?。。?.x版本不再使用 spring.factories做自動配置)
但是這個在真正開始說要棄用,是在 2.7版本。只是向下兼容了 spring.factories 的配置方式。
也就是說兩種寫法共存,如下圖:

在 META-INF 目錄下增加了 spring 目錄,其中有文件 org.springframework.boot.autoconfigure.AutoConfiguration.imports
文件內(nèi)容是配置類的完整類名。
比如我當前使用的配置內(nèi)容是:
org.feng.config.AppInfoConfiguration
而 spring.factories 中的配置內(nèi)容和之前一致:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=org.feng.config.AppInfoConfiguration
這一點其實也可以從 Spring 的源碼包中看到:


本次練習(xí)的代碼倉庫
https://gitee.com/fengsoshuai/custom-springboot-starter-demo.git
代碼簡要說明
| 模塊 | 說明 |
|---|---|
| customer-starter | 自定義的 starter,并提供配置、示例接口&實現(xiàn)類 |
| test | 測試自定義starter,引入自定義starter的依賴,并定義了啟動類,控制器類 |
custom-springboot-starter-demo 的pom文件
主要定義了 SpringBoot的版本。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>custom-springboot-starter-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>custom-springboot-starter-demo</name>
<url>http://maven.apache.org</url>
<modules>
<module>custom-starter</module>
<module>test</module>
</modules>
<properties>
<java.version>11</java.version>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<springboot.dependencies.version>2.7.8</springboot.dependencies.version>
</properties>
<dependencyManagement>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${springboot.dependencies.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>11</source>
<target>11</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>customer-starter 的pom文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.example</groupId>
<artifactId>custom-springboot-starter-demo</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>custom-starter</artifactId>
<packaging>jar</packaging>
<name>custom-starter</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<!-- 將自定義starter中的默認配置文件也打包 -->
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.yml</include>
<include>**/*.yaml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>
test 的pom文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.example</groupId>
<artifactId>custom-springboot-starter-demo</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>test</artifactId>
<packaging>jar</packaging>
<name>test</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.example</groupId>
<artifactId>custom-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
配置類
package org.feng.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.context.annotation.Bean;
/**
* 自動配置類:在META-INF中不做配置時,會拋出警告 <code>Application context not configured for this file</code>
*
* @version V1.0
*/
@AutoConfiguration
public class AppInfoConfiguration {
@Value("${app.url.baiDu}")
private String baiDuUrl;
public String getBaiDuUrl() {
return baiDuUrl;
}
public void setBaiDuUrl(String baiDuUrl) {
this.baiDuUrl = baiDuUrl;
}
@Bean
public AppUrl generateAppUrl() {
AppUrl appUrl = new AppUrl();
appUrl.setBaidu(baiDuUrl);
return appUrl;
}
}
配置信息

到此這篇關(guān)于SpringBoot自定義Starter與自動配置實現(xiàn)方法詳解的文章就介紹到這了,更多相關(guān)SpringBoot自定義Starter內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于spring依賴注入的方式以及優(yōu)缺點
這篇文章主要介紹了關(guān)于spring依賴注入的方式以及優(yōu)缺點,依賴注入,是IOC的一個方面,是個通常的概念,它有多種解釋,這概念是說你不用創(chuàng)建對象,而只需要描述它如何被創(chuàng)建,需要的朋友可以參考下2023-07-07
mybatis foreach 循環(huán) list(map)實例
這篇文章主要介紹了mybatis foreach 循環(huán) list(map)實例,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
淺析SpringBoot統(tǒng)一返回結(jié)果的實現(xiàn)
前后端開發(fā)過程中數(shù)據(jù)交互規(guī)范化是一件非常重要的事情,不僅可以減少前后端交互過程中出現(xiàn)的問題,也讓代碼邏輯更加具有條理,下面小編就和大家講講SpringBoot如何統(tǒng)一返回結(jié)果的吧2023-07-07
詳解SpringBoot如何優(yōu)雅的進行全局異常處理
在SpringBoot的開發(fā)中,為了提高程序運行的魯棒性,我們經(jīng)常需要對各種程序異常進行處理,但是如果在每個出異常的地方進行單獨處理的話,這會引入大量業(yè)務(wù)不相關(guān)的異常處理代碼,這篇文章帶大家了解一下如何優(yōu)雅的進行全局異常處理2023-07-07
SpringCloud Gateway動態(tài)路由配置詳解
這篇文章主要為大家介紹了SpringCloud Gateway動態(tài)路由配置詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-03-03
SpringBoot引入Redis報Redis?command?timed?out兩種異常情況
這篇文章主要給大家介紹了關(guān)于SpringBoot引入Redis報Redis?command?timed?out兩種異常情況的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下2023-08-08
如何將復(fù)雜SQL轉(zhuǎn)換成Java對象的實例講解
轉(zhuǎn)換復(fù)雜SQL到Java代碼,我們需要確定數(shù)據(jù)庫連接方式和工具,使用JDBC的API來連接數(shù)據(jù)庫、執(zhí)行SQL語句,復(fù)雜SQL語句可以被拆分為多個步驟,每個步驟執(zhí)行一個特定的操作,通過將SQL語句拆分為多個步驟,我們可以更好地理解復(fù)雜SQL的邏輯,并且更容易將其轉(zhuǎn)換為Java代碼2024-05-05

