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)截圖

第三步、寫代碼,對(duì)外提供一些自己寫的類
創(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)分開,還有回車小心前面的空格,最好沒有其它字符。

例如:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ top.huashengshu.myspringbootstarter.HelloServiceAutoConfiguration,\ top.yumbo.music.starter.configuration.YumboMusicAutoConfiguration
第五步、將該項(xiàng)目發(fā)布的maven倉庫,或者安裝到本地倉庫中讓其它項(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)目,訪問/hello驗(yàn)證是否有效
如下:

說明自定義starter成功。
其它業(yè)務(wù)代碼,根據(jù)自己的需求自己加入依賴,也就是說可以自己定義starter提供給其它人用!
到此這篇關(guān)于springboot自定義starter啟動(dòng)器的具體使用實(shí)踐的文章就介紹到這了,更多相關(guān)springboot自定義starter啟動(dòng)器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MyBatis Mapper.xml入?yún)ist使用in函數(shù)問題
文章主要講述了在使用MyBatis的Mapper.xml文件時(shí),如何正確地在in函數(shù)中使用List作為入?yún)?作者強(qiáng)調(diào)了完整拷貝<if>...</if>格式的重要性,并指出稍微的改動(dòng)就會(huì)導(dǎo)致錯(cuò)誤2025-02-02
Java 語言實(shí)現(xiàn)清除帶 html 標(biāo)簽的內(nèi)容方法
下面小編就為大家?guī)硪黄狫ava 語言實(shí)現(xiàn)清除帶 html 標(biāo)簽的內(nèi)容方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02
java中關(guān)于轉(zhuǎn)義字符的一個(gè)bug
本文主要介紹了java中關(guān)于轉(zhuǎn)義字符的一個(gè)bug。具有很好的參考價(jià)值,下面跟著小編一起來看下吧2017-02-02
SpringBoot工程Docker多環(huán)境中使用同一個(gè)Jar包解決方案
在Docker多環(huán)境部署中,SpringBoot工程可以通過環(huán)境變量來動(dòng)態(tài)改變配置,無需重新打包,利用volume掛載或docker?cp命令,可以將配置文件直接傳入容器,提高部署效率,并保證安全性2024-09-09
java ArrayList集合中的某個(gè)對(duì)象屬性進(jìn)行排序的實(shí)現(xiàn)代碼
這篇文章主要介紹了java ArrayList集合中的某個(gè)對(duì)象屬性進(jìn)行排序的實(shí)現(xiàn)代碼,需要的朋友可以參考下2016-07-07
SpringBoot中@PostConstruct 注解的實(shí)現(xiàn)
在Spring Boot框架中,?@PostConstruct是一個(gè)非常有用的注解,它用于在依賴注入完成后執(zhí)行初始化方法,本文將介紹?@PostConstruct的基本概念、使用場景以及提供詳細(xì)的代碼示例,感興趣的可以了解一下2024-09-09

