springboot自定義starter實(shí)現(xiàn)過(guò)程圖解
這篇文章主要介紹了springboot自定義starter實(shí)現(xiàn)過(guò)程圖解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
1、創(chuàng)建一個(gè)Empty Project
2、在該工程中點(diǎn)擊+,選擇new module,新建一個(gè)maven工程



點(diǎn)擊確定。
3、在該工程中點(diǎn)擊+,選擇new module,新建一個(gè)Spring Initializr工程

后面直接默認(rèn)next,然后點(diǎn)擊finishi。
兩個(gè)都創(chuàng)建完畢之后點(diǎn)擊apply,點(diǎn)擊OK。得到如下結(jié)構(gòu):

4、在gong-spring-boot-starter中引入gong-spring-boot-starter-autoconfigurer,即在gong-spring-boot-starter的pom.xml中
<?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.gong.starter</groupId>
<artifactId>gong.spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!--引入自動(dòng)配置模塊-->
<dependency>
<groupId>com.gong.starter</groupId>
<artifactId>gong-spring-boot-starter-autoconfigurer</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
我們看下gong-spring-boot-starter-autoconfigurer中的pom.xml
<?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.2.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.gong.starter</groupId>
<artifactId>gong-spring-boot-starter-autoconfigurer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>gong-spring-boot-starter-autoconfigurer</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--引入spring-boot-starter:所有starter基本配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
</project>
刪除掉創(chuàng)建項(xiàng)目時(shí)自動(dòng)配置的其它東西,只需要一個(gè)標(biāo)紅的依賴即可。
5、在gong-spring-boot-starter-autoconfigurer就可以編寫場(chǎng)景啟動(dòng)的相關(guān)邏輯啦。
(1)新建如下目錄結(jié)構(gòu)及文件

springboot啟動(dòng)入口可以刪去,resources下文件刪去,test文件夾刪去。在com.gong.starter下新建以上三個(gè)java文件,在resources下新建META-INF文件夾,再新建spring.factories文件。
HelloService.java
package com.gong.starter;
public class HelloService {
HelloProperties helloProperties;
public HelloProperties getHelloProperties() {
return helloProperties;
}
public void setHelloProperties(HelloProperties helloProperties) {
this.helloProperties = helloProperties;
}
public String sayHelloGong(String name){
return helloProperties.getPrefix()+"-" +name + helloProperties.getSuffix();
}
}
sayHelloGong方法可以獲取HelloProperties中的屬性,包括前綴和后綴,然后返回:前綴-name后綴。
HelloProperties.java
package com.gong.starter;
import org.springframework.boot.context.properties.ConfigurationProperties;
//綁定所有以gong.hello開(kāi)頭的配置
@ConfigurationProperties(prefix = "gong.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;
}
}
綁定配置以及定義屬性。
HelloServiceAutoConfiguration.java
package com.gong.starter;
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 //是一個(gè)配置類
@ConditionalOnWebApplication //web應(yīng)用才生效
@EnableConfigurationProperties(HelloProperties.class) //讓屬性文件生效
public class HelloServiceAutoConfiguration {
@Autowired
HelloProperties helloProperties;
@Bean
public HelloService helloService(){
HelloService service = new HelloService();
service.setHelloProperties(helloProperties);
return service;
}
}
自動(dòng)配置類。要加入三個(gè)注解,并對(duì)方法使用@Bean標(biāo)注。
最后,就是在spring.factories中進(jìn)行配置自動(dòng)注冊(cè):
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.gong.starter.HelloServiceAutoConfiguration
這樣,我們自己定義的場(chǎng)景啟動(dòng)器就完成了。
接下來(lái)新建一個(gè)springboot項(xiàng)目進(jìn)行測(cè)試,首先在pom.xml中導(dǎo)入自己定義的場(chǎng)景啟動(dòng)器:
<!--引入自定義的starter-->
<dependency>
<groupId>com.gong.starter</groupId>
<artifactId>gong.spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
然后編寫application.properties定義場(chǎng)景啟動(dòng)器的屬性:
gong.hello.prefix=GONG gong.hello.suffix=HELLO WORLD
接著編寫一個(gè)測(cè)試類:
package com.gong.springbootjpa.controller;
import com.gong.starter.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Autowired
HelloService helloService;
@GetMapping("/hello")
public String hello(){
return helloService.sayHelloGong("haha");
}
}
啟動(dòng)服務(wù)器:

完美。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- spring boot 自定義starter的實(shí)現(xiàn)教程
- springboot自定義Starter的具體流程
- Spring boot創(chuàng)建自定義starter的完整步驟
- spring boot微服務(wù)自定義starter原理詳解
- springboot自定義redis-starter的實(shí)現(xiàn)
- SpringBoot自動(dòng)配置之自定義starter的實(shí)現(xiàn)代碼
- 使用SpringBoot自定義starter的完整步驟
- Java SpringBoot自定義starter詳解
- SpringBoot如何自定義starter
- SpringBoot自定義start詳細(xì)圖文教程
相關(guān)文章
java使用apache.poi導(dǎo)出word文件的示例代碼
這篇文章主要介紹了java使用apache.poi導(dǎo)出word文件,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07
Java編程實(shí)現(xiàn)鄰接矩陣表示稠密圖代碼示例
這篇文章主要介紹了Java編程實(shí)現(xiàn)鄰接矩陣表示稠密圖代碼示例,具有一定參考價(jià)值,需要的朋友可以了解下。2017-11-11
教你使用IDEA搭建spring源碼閱讀環(huán)境的詳細(xì)步驟
這篇文章主要介紹了使用IDEA搭建spring源碼閱讀環(huán)境的詳細(xì)步驟,本文分兩步通過(guò)實(shí)例圖文相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2021-08-08
微信公眾號(hào)測(cè)試賬號(hào)自定義菜單的實(shí)例代碼
這篇文章主要介紹了微信公眾號(hào)測(cè)試賬號(hào)自定義菜單的實(shí)例代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-02-02
java實(shí)現(xiàn)去除ArrayList重復(fù)字符串
本文主要介紹了java實(shí)現(xiàn)去除ArrayList重復(fù)字符串,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-09-09
詳解Idea SpringBoot搭建SpringCloud的準(zhǔn)備工作(推薦)
這篇文章主要介紹了Idea SpringBoot搭建SpringCloud的準(zhǔn)備工作(推薦),本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10
java后端把數(shù)據(jù)轉(zhuǎn)換為樹(shù),map遞歸生成json樹(shù),返回給前端(后臺(tái)轉(zhuǎn)換)
這篇文章主要介紹了java后端把數(shù)據(jù)轉(zhuǎn)換為樹(shù),map遞歸生成json樹(shù),返回給前端實(shí)例(后臺(tái)轉(zhuǎn)換),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-01-01
JavaSE基礎(chǔ)之反射機(jī)制(反射Class)詳解
反射機(jī)制有什么用?通過(guò)java語(yǔ)言中的反射機(jī)制可以操作字節(jié)碼文件,可以讀和修改字節(jié)碼文件。所以本文將為大家講講反射機(jī)制的使用,需要的可以參考一下2022-09-09

