springboot自定義starter示例代碼
springboot 約定規(guī)范
Starter項(xiàng)目的命名規(guī)范
建議自定義的starter 以 xxx-spring-boot-starter 命名,官方的Starter一般都是以spring-boot-starter-為前綴。
這樣做的目的是為了避免與官方或其他第三方提供的Starter產(chǎn)生沖突或混淆。
Starter項(xiàng)目的結(jié)構(gòu)規(guī)范(重要)
最核心的是 Spring Boot自動(dòng)裝配文件
作用: 用來(lái)指定我們的自動(dòng)配置類(lèi),讓Spring Boot能夠在啟動(dòng)時(shí)自動(dòng)掃描并加載它。
名稱(chēng)必須為 spring.factories
路徑必須為resources/META-INF/spring.factories 這是springboot的約定規(guī)范,不遵守一律失效。
- 1、在spring boot2.7版本之前:
通過(guò)META-INF/spring.factories文件定義我們自動(dòng)配置的類(lèi)。
- 2、在spring boot2.7~spring boot3.0版本之間,是兼容了
META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports 和META-INF/spring.factories 這兩個(gè)文件的。
- 3、在spring boot3.0版本之后,只支持使用
META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports來(lái)自定義我們的自動(dòng)配置的類(lèi)。
注意!!!:springboot 2.7 版本有所變化,具體請(qǐng)查看springboot官網(wǎng)。
- hello-spring-boot-starter
pom.xml
依賴說(shuō)明
- spring-boot-configuration-processor : 編譯時(shí)依賴 可以幫助我們生成屬性類(lèi)和配置元數(shù)據(jù),并且設(shè)置為可選依賴,避免傳遞給其他項(xiàng)目。
- spring-boot-starter : 基礎(chǔ)依賴 提供了Spring Boot核心功能和默認(rèn)配置
<?xml version="1.0" encoding="UTF-8"?> <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> <!-- 用戶依賴必須指定的參數(shù) --> <groupId>org.example</groupId> <artifactId>hello-spring-boot-starter</artifactId> <version>1.0-SNAPSHOT</version> <properties> <!-- jdk版本--> <java.version>1.8</java.version> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <dependencies> <!-- 基礎(chǔ)依賴 提供了Spring Boot核心功能和默認(rèn)配置 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <version>2.7.18</version> </dependency> <!-- 編譯時(shí)依賴 可以幫助我們生成屬性類(lèi)和配置元數(shù)據(jù),并且設(shè)置為可選依賴,避免傳遞給其他項(xiàng)目。--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <version>2.7.18</version> <optional>true</optional> </dependency> </dependencies> </project>
Starter項(xiàng)目的屬性類(lèi)
在創(chuàng)建一個(gè)自定義的Starter項(xiàng)目時(shí),我們需要編寫(xiě)一個(gè)屬性類(lèi),用來(lái)定義我們要集成的功能模塊所需的配置項(xiàng),并且使用@ConfigurationProperties注解來(lái)指定用戶配置文件中的前綴。
package com.hello.starter.properties; /** * * 描述:配置信息 實(shí)體 * @author yss * @date 2024/5/1 */ import org.springframework.boot.context.properties.ConfigurationProperties; @ConfigurationProperties(prefix = "demo") // 指定 用戶配置文件中的前綴 public class DemoProperties { private String sayWhat; private String toWho; public String getSayWhat() { return sayWhat; } public void setSayWhat(String sayWhat) { this.sayWhat = sayWhat; } public String getToWho() { return toWho; } public void setToWho(String toWho) { this.toWho = toWho; } }
Starter項(xiàng)目的業(yè)務(wù)功能類(lèi)
在創(chuàng)建一個(gè)自定義的Starter項(xiàng)目時(shí),我們需要編寫(xiě)一個(gè)或多個(gè)業(yè)務(wù)功能類(lèi),用來(lái)實(shí)現(xiàn)我們要集成的功能模塊的具體邏輯。
package com.hello.starter.service; /** * @author yss * @date 2024/5/1 */ public class DemoService { public String sayWhat; public String toWho; public DemoService(String sayWhat, String toWho){ this.sayWhat = sayWhat; this.toWho = toWho; } public String say(){ return this.sayWhat + "! " + toWho; } }
Starter項(xiàng)目的自動(dòng)配置類(lèi)(重要)
在創(chuàng)建一個(gè)自定義的Starter項(xiàng)目時(shí),我們需要編寫(xiě)一個(gè)自動(dòng)配置類(lèi),用來(lái)根據(jù)屬性類(lèi)和業(yè)務(wù)功能類(lèi),創(chuàng)建相應(yīng)的Bean對(duì)象。Springboot自動(dòng)配置原理源碼解讀
- @EnableConfigurationProperties : 啟用屬性類(lèi),并將其注入到配置類(lèi)中。
- @ConditionalOnProperty: 判斷用戶配置文件中是否有相應(yīng)的配置項(xiàng),存在并且符合期望則滿足條件
- @Configuration : 標(biāo)識(shí)這是一個(gè)配置類(lèi),用來(lái)創(chuàng)建和注冊(cè)Bean對(duì)象。
- @Bean: 根據(jù)屬性類(lèi)和業(yè)務(wù)功能類(lèi),創(chuàng)建相應(yīng)類(lèi)型的Bean對(duì)象,并注冊(cè)到應(yīng)用上下文中。
- @ConditionalOnClass: 判斷業(yè)務(wù)功能類(lèi)是否存在
package com.hello.starter.config; import com.hello.starter.properties.DemoProperties; import com.hello.starter.service.DemoService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * @author yss * @date 2024/5/1 */ @Configuration // 標(biāo)識(shí)這是一個(gè)配置類(lèi),用來(lái)創(chuàng)建和注冊(cè)Bean對(duì)象。 //啟用屬性類(lèi),并將其注入到配置類(lèi)中。 @EnableConfigurationProperties(DemoProperties.class) // 判斷業(yè)務(wù)功能類(lèi)是否存在 @ConditionalOnClass(DemoService.class) // 判斷用戶配置文件中是否存在指定的屬性(isopen), // 如果存在并且值與期望相符, 則滿足條件(開(kāi)啟相應(yīng)功能)。 @ConditionalOnProperty( prefix = "demo", name = "isopen", havingValue = "true" ) public class DemoConfig { @Autowired private DemoProperties demoProperties; // 根據(jù)屬性類(lèi)和業(yè)務(wù)功能類(lèi),創(chuàng)建相應(yīng)類(lèi)型的Bean對(duì)象,并注冊(cè)到應(yīng)用上下文中。 @Bean(name = "demo") public DemoService demoService(){ return new DemoService(demoProperties.getSayWhat(), demoProperties.getToWho()); } }
Starter項(xiàng)目的自動(dòng)裝配文件(重要)
在resources/META-INF目錄下創(chuàng)建一個(gè)名為spring.factories的文件,用來(lái)指定我們的自動(dòng)配置類(lèi),讓Spring Boot能夠在啟動(dòng)時(shí)自動(dòng)掃描并加載它。
以下是一個(gè)示例:
#-------starter自動(dòng)裝配--------- org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.hello.starter.config.DemoConfig
打包發(fā)布
執(zhí)行 mvn clean install 命令 一個(gè)自定義的starter就完成了。
做完上面這幾步,我們自定義Starter就完成了,下面我們來(lái)測(cè)試一下
引入依賴
在我們需要的項(xiàng)目中引入依賴。
<!-- 自定義Starter--> <dependency> <groupId>org.example</groupId> <artifactId>hello-spring-boot-starter</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
- application.yml
demo: # 允許 demo作為bean注入IOC容器 一定要指明 isopen 不然項(xiàng)目無(wú)法啟動(dòng) isopen: true say-what: hello to-who: shf
- DemoController.java
package com.example.testStarter; import com.hello.starter.service.DemoService; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; /** * @author yss * @date 2024/5/1 */ @RestController public class DemoController { @Resource(name = "demo") private DemoService demoService; @RequestMapping("/say") public String sayWhat(){ return demoService.say(); } }
啟動(dòng)項(xiàng)目并且運(yùn)行
ok了??!
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java中的ConcurrentLinkedQueue使用解析
這篇文章主要介紹了Java中的ConcurrentLinkedQueue使用解析,一個(gè)基于鏈接節(jié)點(diǎn)的無(wú)界線程安全隊(duì)列,此隊(duì)列按照 FIFO(先進(jìn)先出)原則對(duì)元素進(jìn)行排序,隊(duì)列的頭部是隊(duì)列中時(shí)間最長(zhǎng)的元素,需要的朋友可以參考下2023-12-12SpringBoot配置Redis實(shí)現(xiàn)保存獲取和刪除數(shù)據(jù)
本文主要介紹了SpringBoot配置Redis實(shí)現(xiàn)保存獲取和刪除數(shù)據(jù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06java實(shí)現(xiàn)猜數(shù)字小游戲(Swing版)
這篇文章主要介紹了java實(shí)現(xiàn)猜數(shù)字小游戲,Swing編程版的猜數(shù)字游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-05-05解決mybatis where-if中if不能識(shí)別大寫(xiě)AND,OR的問(wèn)題
這篇文章主要介紹了解決mybatis where-if中if不能識(shí)別大寫(xiě)AND,OR的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-02-02mybatis中使用not?in與?in的寫(xiě)法說(shuō)明
這篇文章主要介紹了mybatis中使用not?in與?in的寫(xiě)法說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01Java雜談之如何優(yōu)化寫(xiě)出漂亮高效的代碼
不一致的代碼會(huì)造成認(rèn)知上的負(fù)擔(dān),在一個(gè)系統(tǒng)中,做類(lèi)似的事情,卻有不同的做法,或者起到類(lèi)似作用的事物,卻有不同的名字,讓人困惑2021-09-09java 2d畫(huà)圖示例分享(用java畫(huà)圖)
這篇文章主要介紹了java 2D畫(huà)圖示例(用java畫(huà)圖),需要的朋友可以參考下2014-04-04