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

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


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

