springboot自定義starter啟動(dòng)器的具體使用實(shí)踐
第一步、創(chuàng)建 xxx-spring-boot-starter 的spring Initializr模塊




第二步、刪除不需要的內(nèi)容(啟動(dòng)類、除下面spring-boot-starter的其它依賴,maven編譯插件)
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency>
如下是完整的pom.xml
實(shí)際上如果當(dāng)前starter需要引用其它依賴加入到dependences里面即可,這里只做演示項(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 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>
<!-- 保留這個(gè)依賴即可,其它依賴都刪除 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
</project>
項(xiàng)目結(jié)構(gòu)截圖

第三步、寫(xiě)代碼,對(duì)外提供一些自己寫(xiě)的類
創(chuàng)建HelloProperties.java,直接復(fù)制下面代碼,然后選擇包進(jìn)行粘貼,Idea會(huì)自動(dòng)創(chuàng)建對(duì)應(yīng)類代碼設(shè)置好包名
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "hello") // 對(duì)外提供的前綴,相當(dāng)于其它引入當(dāng)前starter在properties文件使用hello.屬性即可對(duì)下面屬性進(jìn)行賦值
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ù)制下面代碼,選擇包進(jìn)行粘貼即可生成
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方法返會(huì)的字符串加上前綴和后綴
@Bean
public HelloService helloService() { // 將HelloService注入到IOC容器
HelloService service = new HelloService();
service.setHelloProperties(helloProperties);
return service;
}
}
第四步、在resources資源文件夾下創(chuàng)建一個(gè)META-INF文件夾,并創(chuàng)建一個(gè)spring.factories文件
如下面截圖

內(nèi)容則是將@Configuration配置類加入,目的是將配置加入到外部的IOC容器中
org.springframework.boot.autoconfigure.EnableAutoConfiguration=
idea中右鍵copy–》copy reference,將復(fù)制的值填入上面=右邊
注意:如果有多個(gè)AutoConfiguration則用逗號(hào)分開(kāi),還有回車(chē)小心前面的空格,最好沒(méi)有其它字符。

例如:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ top.huashengshu.myspringbootstarter.HelloServiceAutoConfiguration,\ top.yumbo.music.starter.configuration.YumboMusicAutoConfiguration
第五步、將該項(xiàng)目發(fā)布的maven倉(cāng)庫(kù),或者安裝到本地倉(cāng)庫(kù)中讓其它項(xiàng)目能使用的到
本地安裝為例:

成功后即可

第六步、測(cè)試自己定義的啟動(dòng)器使用有效
創(chuàng)建一個(gè)springboot項(xiàng)目
勾選web模塊即可,然后加入自定義啟動(dòng)器的gav依賴
在啟動(dòng)類中加入內(nèi)部類(這里為了方便演示不按照規(guī)范創(chuàng)建包)
如下示例代碼
啟動(dò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") // 暴露一個(gè)/hello 請(qǐng)求路徑對(duì)外提供服務(wù)
public String hello(){
return helloService.sayHello("zhang san"); // 返回帶有前綴和后綴中間是 "zhang san"的字符串
}
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
properties文件
因?yàn)槭褂昧?code>@ConfigurationProperties(prefix = "hello")注解所以在當(dāng)前項(xiàng)目的properties文件中使用hello前綴調(diào)用即可對(duì)成員屬性賦值

如下
hello.prefix=HUASHENGSHU hello.suffix=Hello World
運(yùn)行當(dāng)前項(xiàng)目,訪問(wèn)/hello驗(yàn)證是否有效
如下:

說(shuō)明自定義starter成功。
其它業(yè)務(wù)代碼,根據(jù)自己的需求自己加入依賴,也就是說(shuō)可以自己定義starter提供給其它人用!
到此這篇關(guān)于springboot自定義starter啟動(dòng)器的具體使用實(shí)踐的文章就介紹到這了,更多相關(guān)springboot自定義starter啟動(dòng)器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot自定義FailureAnalyzer過(guò)程解析
這篇文章主要介紹了SpringBoot自定義FailureAnalyzer,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11
Clojure?與Java對(duì)比少數(shù)據(jù)結(jié)構(gòu)多函數(shù)勝過(guò)多個(gè)單獨(dú)類的優(yōu)點(diǎn)
這篇文章主要介紹了Clojure?與Java對(duì)比少數(shù)據(jù)結(jié)構(gòu)多函數(shù)勝過(guò)多個(gè)單獨(dú)類的優(yōu)點(diǎn),在Clojure中,我們一次又一次地使用相同的數(shù)據(jù)結(jié)構(gòu),并在其上運(yùn)行許多函,更多相關(guān)介紹需要的朋友可以參考一下下面文章內(nèi)容2022-06-06
IDEA教程創(chuàng)建SpringBoot前后端分離項(xiàng)目示例圖解
在使用spring、mybatis等框架時(shí),配置文件很復(fù)雜,有時(shí)復(fù)雜的讓人想放棄Java,使用C#。springboot出現(xiàn)這一切問(wèn)題就都不是問(wèn)題2021-10-10
利用Java實(shí)現(xiàn)mTLS調(diào)用
這篇文章主要介紹使用 Java作為客戶端 與受 mTLS 保護(hù)的服務(wù)交互。為了對(duì)我們的 Java 客戶端進(jìn)行 ssl 配置,我們需要先設(shè)置一個(gè) SSLContext。這簡(jiǎn)化了事情,因?yàn)?SSLContext 可用于各種 http 客戶端,接下來(lái)我們一起進(jìn)入下面文章了解具體內(nèi)容,需要的朋友可以參考一下2021-11-11
Spring?Boot?內(nèi)置工具類ReflectionUtils的實(shí)現(xiàn)
ReflectionUtils是一個(gè)反射工具類,它封裝了Java反射的操作,使得我們能夠更輕松地操作和訪問(wèn)類的方法、字段,本文主要介紹了Spring?Boot?內(nèi)置工具類ReflectionUtils的實(shí)現(xiàn),感興趣的可以了解一下2023-11-11
Spring引入外部屬性文件配置數(shù)據(jù)庫(kù)連接的步驟詳解
這篇文章主要介紹了Spring引入外部屬性文件配置數(shù)據(jù)庫(kù)連接的步驟詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
詳解Java數(shù)據(jù)結(jié)構(gòu)之平衡二叉樹(shù)
平衡二叉樹(shù)(Balanced?Binary?Tree)又被稱為AVL樹(shù)(有別于AVL算法),且具有以下性質(zhì):它是一?棵空樹(shù)或它的左右兩個(gè)子樹(shù)的高度差的絕對(duì)值不超過(guò)1,并且左右兩個(gè)子樹(shù)都是一棵平衡二叉樹(shù)。本文將詳解介紹一下平衡二叉樹(shù)的原理與實(shí)現(xiàn),需要的可以參考一下2022-02-02
java環(huán)境變量的配置方法圖文詳解【win10環(huán)境為例】
這篇文章主要介紹了java環(huán)境變量的配置方法,結(jié)合圖文形式詳細(xì)分析了win10環(huán)境下java環(huán)境變量的配置方法與相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2020-04-04

