SpringBoot自定義starter啟動(dòng)器的實(shí)現(xiàn)思路
一.引言
相信現(xiàn)在有很多小伙伴都已經(jīng)很熟悉SpringBoot技術(shù)了。它大大地簡(jiǎn)化了Spring應(yīng)用的開(kāi)發(fā),極大地提高了項(xiàng)目的開(kāi)發(fā)效率,受到廣大開(kāi)發(fā)者和企業(yè)的青睞。特別是SpringBoot官方針對(duì)各種不同的應(yīng)用場(chǎng)景,提供了非常豐富的場(chǎng)景啟動(dòng)器(也稱為起步依賴)。開(kāi)發(fā)人員只需要在項(xiàng)目的POM文件中導(dǎo)入對(duì)應(yīng)的場(chǎng)景依賴,并編寫(xiě)少量的配置,即可快速實(shí)現(xiàn)當(dāng)前場(chǎng)景的應(yīng)用開(kāi)發(fā),真正的實(shí)現(xiàn)開(kāi)箱即用。
今天壹哥會(huì)通過(guò)這篇文章,并結(jié)合一個(gè)具體的案例來(lái)給各位小伙伴介紹一下,我們?cè)撊绾巫远x一個(gè)自己的SpringBoot場(chǎng)景啟動(dòng)器,畢竟有時(shí)候官方提供的starter不能完全滿足我們所有的需求。同時(shí)壹哥也希望通過(guò)starter的自定義過(guò)程,能夠加深大家對(duì)SpringBoot自動(dòng)配置原理的理解。
二. 需求說(shuō)明
我們先來(lái)看一段代碼:
package com.qf.hello.service; import com.qf.hello.bean.HelloProperties; import org.springframework.beans.factory.annotation.Autowired; public class HelloService { @Autowired HelloProperties helloProperties; public String sayHello(String name){ return helloProperties.getPrefix() + ":" + name + ">>>" + helloProperties.getSuffix(); } }
上面我們定義了一個(gè)組件HelloService,它有一個(gè)非常簡(jiǎn)單的功能,就是能夠根據(jù)調(diào)用者傳遞的名字返回一個(gè)打招呼的信息,返回的信息內(nèi)容可以根據(jù)配置的前綴和后綴進(jìn)行指定格式的設(shè)置。我們現(xiàn)在需要將這個(gè)功能做成一個(gè)Starter,將來(lái)在其他項(xiàng)目中可以直接以場(chǎng)景啟動(dòng)器的方式導(dǎo)入并使用。
三. 設(shè)計(jì)思路
回顧我們之前使用已經(jīng)做好的starter,你會(huì)發(fā)現(xiàn)無(wú)非就是如下幾個(gè)步驟:
在POM文件中導(dǎo)入場(chǎng)景依賴;
這個(gè)場(chǎng)景依賴中,包含了一個(gè)名為xxxAutoConfiguration的自動(dòng)配置類(lèi);
自動(dòng)配置類(lèi)按照一定的條件進(jìn)行相關(guān)組件的自動(dòng)裝配;
這些組件又綁定了名為xxxProperties屬性配置類(lèi);
屬性配置類(lèi)通過(guò)指定的前綴,從application.yml配置文件中讀取屬性的配置信息;
最后在項(xiàng)目中直接使用這些配置好的組件。
我們就參考這個(gè)步驟開(kāi)始進(jìn)行自定義starter的操作。
四. 實(shí)現(xiàn)步驟
1. Step1 業(yè)務(wù)定義
創(chuàng)建一個(gè)空項(xiàng)目【customer-starter】,里面包含兩個(gè)模塊:
啟動(dòng)器模塊【hello-spring-boot-starter】;
自動(dòng)配置模塊【hello-spring-boot-starter-configuration】
其中啟動(dòng)器項(xiàng)目中無(wú)需任何源代碼和配置文件,只需要引入自動(dòng)配置項(xiàng)目的依賴即可。
<?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> <groupId>com.qf</groupId> <artifactId>hello-spring-boot-starter</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>com.qf</groupId> <artifactId>hello-spring-boot-starter-configuration</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> </project>
自動(dòng)配置項(xiàng)目必須是一個(gè)SpringBoot工程,同時(shí)需要引入spring-boot-starter的依賴。
<?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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.5</version> </parent> <groupId>com.qf</groupId> <artifactId>hello-spring-boot-starter-configuration</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> </dependencies> </project>
2. Step2 自動(dòng)配置
編寫(xiě)自動(dòng)配置項(xiàng)目中的內(nèi)容。
HelloService是整個(gè)自定義Starter要裝配的核心對(duì)象,HelloServiceAutoConfiguration是一個(gè)配置類(lèi),HelloProperties是HelloService組件綁定的屬性配置類(lèi),他們的代碼分別如下:
2.1 HelloService類(lèi)
//HelloService:該組件不要默認(rèn)注冊(cè)到容器中,而是通過(guò)一個(gè)自動(dòng)配置類(lèi)按條件進(jìn)行裝配 package com.qf.hello.service; import com.qf.hello.bean.HelloProperties; import org.springframework.beans.factory.annotation.Autowired; public class HelloService { @Autowired HelloProperties helloProperties; public String sayHello(String name){ return helloProperties.getPrefix() + ":" + name + ">>>" + helloProperties.getSuffix(); } }
2.2 HelloProperties類(lèi)
//HelloProperties:自配配置屬性類(lèi) package com.qf.hello.bean; import org.springframework.boot.context.properties.ConfigurationProperties; @ConfigurationProperties(prefix = "hello") public class HelloProperties { //sayHello方法使用的前綴信息 private String prefix; //sayHello方法使用的后綴信息 private String suffix; public String getPrefix() { return prefix; } public void setPrefix(String prefix) { this.prefix = prefix; } public String getSuffix() { return suffix; } public void setSuffix(String suffix) { this.suffix = suffix; } }
2.3 HelloServiceAutoConfiguration類(lèi)
//自動(dòng)配置類(lèi) package com.qf.hello.auto; import com.qf.hello.bean.HelloProperties; import com.qf.hello.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; @Configuration @EnableConfigurationProperties(HelloProperties.class) public class HelloServiceAutoConfiguration { @ConditionalOnMissingBean(HelloService.class) @Bean public HelloService helloService(){ System.out.println("使用自定義starter提供的HelloService"); return new HelloService(); } }
3. Step3 工廠文件
在自動(dòng)配置項(xiàng)目中的resources目錄中,提供一個(gè)名稱為META-INF的目錄,并在該目錄下提供一個(gè)名為spring.factories的文件。
org.springframework.boot.autoconfigure.EnableAutoConfiguration= \com.qf.hello.auto.HelloServiceAutoConfiguration
spring.factories配置的內(nèi)容如下:
org.springframework.boot.autoconfigure.EnableAutoConfiguration= \com.qf.hello.auto.HelloServiceAutoConfiguration
4. Step4 安裝
將這兩個(gè)項(xiàng)目clean,并install到本地倉(cāng)庫(kù)。
5. Step5 引入使用
創(chuàng)建一個(gè)web項(xiàng)目進(jìn)行自定義starter的使用測(cè)試。
5.1 在應(yīng)用中添加自定義starter依賴坐標(biāo)
<!-- 1.引入我們自定義的場(chǎng)景啟動(dòng)器 --> <dependency> <groupId>com.qf</groupId> <artifactId>hello-spring-boot-starter</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
5.2 編寫(xiě)配置信息
server: port: 8080 hello: prefix: 千鋒 suffix: 888
5.3 編寫(xiě)測(cè)試的Controller
并在該Controller中自動(dòng)注入自定義場(chǎng)景啟動(dòng)器中的HelloService組件。
package com.qf.boot.controller; import com.qf.hello.service.HelloService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @Autowired HelloService helloService; @GetMapping("/hello/{name}") public String hello(@PathVariable("name") String name){ return helloService.sayHello(name); } }
5.4 打開(kāi)瀏覽器輸入Controller中定義的訪問(wèn)地址
通過(guò)測(cè)試發(fā)現(xiàn),我們已經(jīng)可以在其他項(xiàng)目中使用自定義的starter,并使用自動(dòng)配置好的組件功能了!現(xiàn)在你知道該怎么自定義starter了嗎?如果你還有其他問(wèn)題,可以在評(píng)論區(qū)留言或私信哦。
到此這篇關(guān)于SpringBoot如何自定義starter啟動(dòng)器的文章就介紹到這了,更多相關(guān)SpringBoot自定義starter啟動(dòng)器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java拖曳鼠標(biāo)實(shí)現(xiàn)畫(huà)線功能的方法
這篇文章主要介紹了Java拖曳鼠標(biāo)實(shí)現(xiàn)畫(huà)線功能的方法,需要的朋友可以參考下2014-07-07mybatis判斷int是否為空的時(shí)候,需要注意的3點(diǎn)
這篇文章主要介紹了mybatis判斷int是否為空的時(shí)候,需要注意的3點(diǎn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07Java之while與do-while循環(huán)的用法詳解
在上一篇文章中,給大家講解了循環(huán)的概念,并重點(diǎn)給大家講解了for循環(huán)的使用。但在Java中,除了for循環(huán)之外,還有while、do-while、foreach等循環(huán)形式。這篇文章給大家講解while循環(huán)的使用2023-05-05JavaWeb項(xiàng)目打開(kāi)網(wǎng)頁(yè)出現(xiàn)Session Error的異常解決方案
這篇文章主要介紹了JavaWeb項(xiàng)目打開(kāi)網(wǎng)頁(yè)出現(xiàn)Session Error的異常解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10SpringBoot集成PostgreSQL并設(shè)置最大連接數(shù)
本文主要介紹了SpringBoot集成PostgreSQL并設(shè)置最大連接數(shù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-11-11解決后端傳long類(lèi)型數(shù)據(jù)到前端精度丟失問(wèn)題
這篇文章主要介紹了解決后端傳long類(lèi)型數(shù)據(jù)到前端精度丟失問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01