Spring?boot?自定義?Starter及自動(dòng)配置的方法
Starter 組件簡(jiǎn)介
Starter 組件是 Spring boot 的一個(gè)核心特性,Starter組件的出現(xiàn)極大的簡(jiǎn)化了項(xiàng)目開發(fā),例如在項(xiàng)目中使用的pom.xml文件中添加以下配置:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.5.3</version>
</dependency>Spring boot 就會(huì)自動(dòng)關(guān)聯(lián)web 開發(fā)相關(guān)的依賴,如 tomcat 及 spring-mvc等,進(jìn)而能支持web開發(fā),同時(shí)相關(guān)技術(shù)也將實(shí)現(xiàn)自動(dòng)配置,避免了繁瑣的配置文件。
Starter 組件 使開發(fā)者不需要關(guān)注各種依賴庫的處理、不需要具體的配置信息,由Spring boot 自動(dòng)完成class的發(fā)現(xiàn)并加載。
利用Starter實(shí)現(xiàn)自動(dòng)化配置需要兩個(gè)條件:Maven依賴以及配置文件。
Maven依賴實(shí)際上就是導(dǎo)入一個(gè)JAR,然后Springboot啟動(dòng)時(shí)候就會(huì)找到這個(gè)jar包中 resource/META-INF/spring.factories文件,根據(jù)文件的配置找到相關(guān)的實(shí)現(xiàn)類。以上就是spring boot 的自動(dòng)裝配機(jī)制,如果理解該機(jī)制的話,很容易就能手寫一個(gè) starter 組件。
對(duì)starter組件我們可以簡(jiǎn)單理解為:
- 每個(gè)不同的starter組件實(shí)際上完成自身的功能邏輯,然后對(duì)外提供一個(gè)bean對(duì)象讓別人調(diào)用
- 對(duì)外提供的bean通過自動(dòng)裝配機(jī)制注入到IOC容器中
自定義 Starter 組件
要實(shí)現(xiàn)一個(gè)自己的 starter 組件其實(shí)也很簡(jiǎn)單,首先我們需要明確我們需要做那些事:
- 通過配置類提供對(duì)外服務(wù)的 bean 對(duì)象
- 按照自動(dòng)裝配原理完成 spring.factories 的編寫
- starter 自動(dòng)屬性配置
Starter 組件
接下來我們就手寫一個(gè) starter 組件,模仿 RedisTemplate,流程如下:
- 創(chuàng)建一個(gè)spring boot 項(xiàng)目
redis-starter-demo - 創(chuàng)建
MyStarterTemplate
簡(jiǎn)單模擬 redis 的 get,set操作:
public class MyStarterTemplate {
Map<String, String> map = new HashMap<>();
public String put(String key, String value) {
map.put(key, value);
return "保存成功";
}
public String get(String key) {
return map.get(key);
}
}- 創(chuàng)建配置類
@Configuration
public class MyAutoConfiguration {
@Bean
public MyStarterTemplate myStarterTemplate(){
return new MyStarterTemplate();
}
}- 實(shí)現(xiàn)自動(dòng)裝配流程
在 classpath 下 META-INF目錄下創(chuàng)建 spring.factories 文件:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.example.redisstarterdemo.MyAutoConfiguration
打包發(fā)布 maven install
使用 Starter
- 創(chuàng)建一個(gè)新項(xiàng)目用來引入 starter
test-demo - pom 文件引入依賴
<dependency>
<groupId>com.example</groupId>
<artifactId>redis-starter-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>編寫接口 ,直接 Autowired 注入MyStarterTemplate
@RestController
@RequestMapping(value = "/myRedis")
public class RedisStarterController {
@Autowired
private MyStarterTemplate myStarterTemplate;
@RequestMapping("/save")
public String save(@RequestParam String key, @RequestParam String value) {
myStarterTemplate.put(key,value);
return "ok";
}
@RequestMapping("/get")
public String get(@RequestParam String key) {
return myStarterTemplate.get(key);
}
}以上便是一個(gè)完成的自定義 Starter,如果搞懂SPI機(jī)制的話,現(xiàn)在應(yīng)該覺得很簡(jiǎn)單單了吧?
Starter 傳參
以上是一個(gè)最簡(jiǎn)單的 starter 組件,但是有這么一種情況,我們還以 redis 為例,在 redis 中我們是可以通過在配置文件中來設(shè)置地址、賬號(hào)密碼等信息的,那這種情況我們應(yīng)該如何操作呢?
- 我們還是在上面的項(xiàng)目中繼續(xù)添加代碼
- 創(chuàng)建需要注入的 bean 類
這里我們通過對(duì)外暴露一個(gè)創(chuàng)建方法用來給對(duì)象傳遞地址和端口號(hào);
public class MyRedis {
public String say() {
return "my host:" + host + ",port:" + port;
}
public static MyRedis create(String host, Integer port) {
return new MyRedis(host, port);
}
public MyRedis(String host, Integer port) {
this.host = host;
this.port = port;
}
private String host;
private Integer port;
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public Integer getPort() {
return port;
}
public void setPort(Integer port) {
this.port = port;
}
}
- 創(chuàng)建屬性類
@ConfigurationProperties(prefix = "redis")的作用是讀取 application中以 redis 開頭的數(shù)據(jù),還有其他幾種讀取配置文件的注解可以自行研究。
由于我們這里設(shè)置了默認(rèn)值,所以如果不配置的話也不影響,會(huì)使用默認(rèn)值傳遞給 bean 。
@ConfigurationProperties(prefix = "redis")
public class MyProperties {
private String host = "127.0.0.1";
private Integer port = 6379;
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public Integer getPort() {
return port;
}
public void setPort(Integer port) {
this.port = port;
}
}
@ConfigurationProperties(prefix = "redis")
public class MyProperties {
private String host = "127.0.0.1";
private Integer port = 6379;
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public Integer getPort() {
return port;
}
public void setPort(Integer port) {
this.port = port;
}
}
- 創(chuàng)建配置類
使用配置文件,將相關(guān)信息傳遞給 bean 類。
@Configuration
@EnableConfigurationProperties(MyProperties.class)
public class MyRedisConfiguration {
@Bean
public MyRedis myRedis(MyProperties properties) {
return MyRedis.create(properties.getHost(), properties.getPort());
}
}- 實(shí)現(xiàn)自動(dòng)裝配流程
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.example.redisstarterdemo.demo1.MyAutoConfiguration,\ com.example.redisstarterdemo.demo2.MyRedisConfiguration
在項(xiàng)目中引用還是同樣的操作,如果想要設(shè)置屬性的話,只需要在配置文件中編寫相關(guān)信息即可:
redis.host=196.128.255.255 redis.port=9999
自身與第三方維護(hù)
針對(duì) spring boot 的starter 組件分為兩類:
1.springboot 自身維護(hù)的starter組件
- 所有的 starter 組件自身不帶 spring.factories 文件,都集中在spring-boot-autoconfigure 包下的 EnableAutoConfiguration
- springboot 裝配這些配置類是由條件的,不會(huì)將所有的都一股腦都加載進(jìn)來,假設(shè)我沒用到 redis 的話就不會(huì)引入相關(guān)依賴包,這樣根據(jù)@ConditionalOnClass(RedisOperations.class)在classpath下找不到RedisOperations類,就不會(huì)加載該配置類。
- 自身維護(hù)的starter組件的命名為:spring-boot-starter-xxx
2.第三方維護(hù)的starter 組件
- 在自己的 jar 中維護(hù) spring.factories 文件
- 命名方式:xxx-spring-boot-starter
到此這篇關(guān)于Spring boot 自定義 Starter 及 自動(dòng)配置的文章就介紹到這了,更多相關(guān)Spring boot 自定義 Starter內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
@RequestBody 部分屬性沒有轉(zhuǎn)化成功的處理
這篇文章主要介紹了@RequestBody 部分屬性沒有轉(zhuǎn)化成功的處理方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
SpringBoot項(xiàng)目啟動(dòng)后再請(qǐng)求遠(yuǎn)程接口的解決方式
Spring?Boot是由Pivotal團(tuán)隊(duì)提供的全新框架,其設(shè)計(jì)目的是用來簡(jiǎn)化Spring應(yīng)用的創(chuàng)建、運(yùn)行、調(diào)試、部署等,這篇文章主要介紹了SpringBoot項(xiàng)目啟動(dòng)后再請(qǐng)求遠(yuǎn)程接口的實(shí)現(xiàn)方式?,需要的朋友可以參考下2023-02-02
使用hibernate和struts2實(shí)現(xiàn)分頁功能的示例
本篇文章主要介紹了使用hibernate和struts2實(shí)現(xiàn)分頁功能,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-01-01
spring-boot整合Micrometer+Prometheus的詳細(xì)過程
這篇文章主要介紹了springboot整合Micrometer+Prometheus的詳細(xì)過程,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-05-05
Java設(shè)計(jì)模式之單例模式簡(jiǎn)介
這篇文章主要介紹了Java設(shè)計(jì)模式之單例模式簡(jiǎn)介,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)Java的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-04-04
IDEA工程運(yùn)行時(shí)總是報(bào)xx程序包不存在實(shí)際上包已導(dǎo)入(問題分析及解決方案)
這篇文章主要介紹了IDEA工程運(yùn)行時(shí),總是報(bào)xx程序包不存在,實(shí)際上包已導(dǎo)入,本文給大家分享問題分析及解決方案,通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2020-08-08
Java Swing中JList選擇事件監(jiān)聽器ListSelectionListener用法示例
這篇文章主要介紹了Java Swing中JList選擇事件監(jiān)聽器ListSelectionListener用法,結(jié)合具體實(shí)例形式分析了中JList選擇事件監(jiān)聽器ListSelectionListener的功能、使用方法及相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-11-11
spring boot搭建文件服務(wù)器解決同時(shí)上傳多個(gè)圖片和下載的問題
這篇文章主要介紹了spring boot搭建文件服務(wù)器解決同時(shí)上傳多個(gè)圖片和下載的問題,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-11-11

