javascript
SpringBoot如何自定义starter启动器?看这里
一. 引言
相信現在有很多小伙伴都已經很熟悉SpringBoot技術了。它大大地簡化了Spring應用的開發,極大地提高了項目的開發效率,受到廣大開發者和企業的青睞。特別是SpringBoot官方針對各種不同的應用場景,提供了非常豐富的場景啟動器(也稱為起步依賴)。開發人員只需要在項目的POM文件中導入對應的場景依賴,并編寫少量的配置,即可快速實現當前場景的應用開發,真正的實現開箱即用。
今天壹哥會通過這篇文章,并結合一個具體的案例來給各位小伙伴介紹一下,我們該如何自定義一個自己的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 {@AutowiredHelloProperties helloProperties;public String sayHello(String name){return helloProperties.getPrefix() + ":" + name + ">>>" + helloProperties.getSuffix();} }上面我們定義了一個組件HelloService,它有一個非常簡單的功能,就是能夠根據調用者傳遞的名字返回一個打招呼的信息,返回的信息內容可以根據配置的前綴和后綴進行指定格式的設置。我們現在需要將這個功能做成一個Starter,將來在其他項目中可以直接以場景啟動器的方式導入并使用。
三. 設計思路
回顧我們之前使用已經做好的starter,你會發現無非就是如下幾個步驟:
在POM文件中導入場景依賴;
這個場景依賴中,包含了一個名為xxxAutoConfiguration的自動配置類;
自動配置類按照一定的條件進行相關組件的自動裝配;
這些組件又綁定了名為xxxProperties屬性配置類;
屬性配置類通過指定的前綴,從application.yml配置文件中讀取屬性的配置信息;
最后在項目中直接使用這些配置好的組件。
我們就參考這個步驟開始進行自定義starter的操作。
四. 實現步驟
1. Step1 業務定義
創建一個空項目【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 自動配置
編寫自動配置項目中的內容。
?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 {@AutowiredHelloProperties 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)@Beanpublic HelloService helloService(){System.out.println("使用自定義starter提供的HelloService");return new HelloService();} }3. Step3 工廠文件
在自動配置項目中的resources目錄中,提供一個名稱為META-INF的目錄,并在該目錄下提供一個名為spring.factories的文件。
resources/META-INF/spring.factories
spring.factories配置的內容如下:
org.springframework.boot.autoconfigure.EnableAutoConfiguration= \com.qf.hello.auto.HelloServiceAutoConfiguration4. Step4 安裝
將這兩個項目clean,并install到本地倉庫。
5. Step5 引入使用
創建一個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: 8885.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 {@AutowiredHelloService helloService;@GetMapping("/hello/{name}")public String hello(@PathVariable("name") String name){return helloService.sayHello(name);} }5.4 打開瀏覽器輸入Controller中定義的訪問地址
通過測試發現,我們已經可以在其他項目中使用自定義的starter,并使用自動配置好的組件功能了!現在你知道該怎么自定義starter了嗎?如果你還有其他問題,可以在評論區留言或私信哦。
總結
以上是生活随笔為你收集整理的SpringBoot如何自定义starter启动器?看这里的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: css中left属性,CSS left属
- 下一篇: 分析C语言的声明——《C专家编程》