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