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 的源碼包中看到:
本次練習的代碼倉庫
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)文章
淺談Spring中@NotEmpty、@NotBlank、@NotNull區(qū)別
本文主要介紹了淺談Spring中@NotEmpty、@NotBlank、@NotNull區(qū)別,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-02-02