springboot自定義starter啟動器的具體使用實踐
第一步、創(chuàng)建 xxx-spring-boot-starter 的spring Initializr模塊
第二步、刪除不需要的內(nèi)容(啟動類、除下面spring-boot-starter的其它依賴,maven編譯插件)
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency>
如下是完整的pom.xml
實際上如果當(dāng)前starter
需要引用其它依賴加入到dependences里面即可,這里只做演示項目
<?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 https://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.3.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>top.huashengshu</groupId> <artifactId>my-spring-boot-starter</artifactId> <version>0.0.1-SNAPSHOT</version> <name>my-spring-boot-starter</name> <description>Demo project for Spring Boot</description> <properties> <java.version>11</java.version> </properties> <dependencies> <!-- 保留這個依賴即可,其它依賴都刪除 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> </dependencies> </project>
項目結(jié)構(gòu)截圖
第三步、寫代碼,對外提供一些自己寫的類
創(chuàng)建HelloProperties.java
,直接復(fù)制下面代碼,然后選擇包進行粘貼,Idea會自動創(chuàng)建對應(yīng)類代碼設(shè)置好包名
import org.springframework.boot.context.properties.ConfigurationProperties; @ConfigurationProperties(prefix = "hello") // 對外提供的前綴,相當(dāng)于其它引入當(dāng)前starter在properties文件使用hello.屬性即可對下面屬性進行賦值 public class HelloProperties { private String prefix; // 成員屬性,意思是前綴名 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; } }
創(chuàng)建HelloService.java
直接復(fù)制下面代碼,選擇包進行粘貼即可生成
public class HelloService { HelloProperties helloProperties; public HelloProperties getHelloProperties() { return helloProperties; } public void setHelloProperties(HelloProperties helloProperties) { this.helloProperties = helloProperties; } public String sayHello(String name){ return helloProperties.getPrefix() +" "+name +" "+helloProperties.getSuffix(); } }
創(chuàng)建配置類(和前面一樣復(fù)制粘貼即可)HelloServiceAutoConfiguration.java
,將HelloService注入到IOC容器中
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration @ConditionalOnWebApplication // 條件配置類,該注解表示在web環(huán)境下才生效,相關(guān)的其它條件可以使用@ConditionXXX @EnableConfigurationProperties(HelloProperties.class) // 表示HelloProperties作為配置類使用 public class HelloServiceAutoConfiguration { @Autowired HelloProperties helloProperties; // 作為配置類目的就是想在sayHello方法返會的字符串加上前綴和后綴 @Bean public HelloService helloService() { // 將HelloService注入到IOC容器 HelloService service = new HelloService(); service.setHelloProperties(helloProperties); return service; } }
第四步、在resources資源文件夾下創(chuàng)建一個META-INF
文件夾,并創(chuàng)建一個spring.factories
文件
如下面截圖
內(nèi)容則是將@Configuration配置類加入,目的是將配置加入到外部的IOC容器中
org.springframework.boot.autoconfigure.EnableAutoConfiguration=
idea中右鍵copy–》copy reference,將復(fù)制的值填入上面=
右邊
注意:如果有多個AutoConfiguration則用逗號分開,還有回車小心前面的空格,最好沒有其它字符。
例如:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ top.huashengshu.myspringbootstarter.HelloServiceAutoConfiguration,\ top.yumbo.music.starter.configuration.YumboMusicAutoConfiguration
第五步、將該項目發(fā)布的maven倉庫,或者安裝到本地倉庫中讓其它項目能使用的到
本地安裝為例:
成功后即可
第六步、測試自己定義的啟動器使用有效
創(chuàng)建一個springboot項目
勾選web模塊即可,然后加入自定義啟動器的gav依賴
在啟動類中加入內(nèi)部類(這里為了方便演示不按照規(guī)范創(chuàng)建包)
如下示例代碼
啟動類
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import top.huashengshu.myspringbootstarter.HelloService; @SpringBootApplication public class DemoApplication { @RestController public class HelloController { @Autowired HelloService helloService; // 注入HelloService @GetMapping("/hello") // 暴露一個/hello 請求路徑對外提供服務(wù) public String hello(){ return helloService.sayHello("zhang san"); // 返回帶有前綴和后綴中間是 "zhang san"的字符串 } } public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
properties文件
因為使用了@ConfigurationProperties(prefix = "hello")
注解所以在當(dāng)前項目的properties文件中使用hello前綴調(diào)用即可對成員屬性賦值
如下
hello.prefix=HUASHENGSHU hello.suffix=Hello World
運行當(dāng)前項目,訪問/hello
驗證是否有效
如下:
說明自定義starter成功。
其它業(yè)務(wù)代碼,根據(jù)自己的需求自己加入依賴,也就是說可以自己定義starter提供給其它人用!
到此這篇關(guān)于springboot自定義starter啟動器的具體使用實踐的文章就介紹到這了,更多相關(guān)springboot自定義starter啟動器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot自定義FailureAnalyzer過程解析
這篇文章主要介紹了SpringBoot自定義FailureAnalyzer,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-11-11Clojure?與Java對比少數(shù)據(jù)結(jié)構(gòu)多函數(shù)勝過多個單獨類的優(yōu)點
這篇文章主要介紹了Clojure?與Java對比少數(shù)據(jù)結(jié)構(gòu)多函數(shù)勝過多個單獨類的優(yōu)點,在Clojure中,我們一次又一次地使用相同的數(shù)據(jù)結(jié)構(gòu),并在其上運行許多函,更多相關(guān)介紹需要的朋友可以參考一下下面文章內(nèi)容2022-06-06IDEA教程創(chuàng)建SpringBoot前后端分離項目示例圖解
在使用spring、mybatis等框架時,配置文件很復(fù)雜,有時復(fù)雜的讓人想放棄Java,使用C#。springboot出現(xiàn)這一切問題就都不是問題2021-10-10Spring?Boot?內(nèi)置工具類ReflectionUtils的實現(xiàn)
ReflectionUtils是一個反射工具類,它封裝了Java反射的操作,使得我們能夠更輕松地操作和訪問類的方法、字段,本文主要介紹了Spring?Boot?內(nèi)置工具類ReflectionUtils的實現(xiàn),感興趣的可以了解一下2023-11-11Spring引入外部屬性文件配置數(shù)據(jù)庫連接的步驟詳解
這篇文章主要介紹了Spring引入外部屬性文件配置數(shù)據(jù)庫連接的步驟詳解,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01詳解Java數(shù)據(jù)結(jié)構(gòu)之平衡二叉樹
平衡二叉樹(Balanced?Binary?Tree)又被稱為AVL樹(有別于AVL算法),且具有以下性質(zhì):它是一?棵空樹或它的左右兩個子樹的高度差的絕對值不超過1,并且左右兩個子樹都是一棵平衡二叉樹。本文將詳解介紹一下平衡二叉樹的原理與實現(xiàn),需要的可以參考一下2022-02-02java環(huán)境變量的配置方法圖文詳解【win10環(huán)境為例】
這篇文章主要介紹了java環(huán)境變量的配置方法,結(jié)合圖文形式詳細(xì)分析了win10環(huán)境下java環(huán)境變量的配置方法與相關(guān)操作注意事項,需要的朋友可以參考下2020-04-04