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)注各種依賴庫(kù)的處理、不需要具體的配置信息,由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)文章
Java編程實(shí)現(xiàn)基于TCP協(xié)議的Socket聊天室示例
這篇文章主要介紹了Java編程實(shí)現(xiàn)基于TCP協(xié)議的Socket聊天室,結(jié)合實(shí)例形式詳細(xì)分析了java基于TCP協(xié)議的Socket聊天室客戶端與服務(wù)器端相關(guān)實(shí)現(xiàn)與使用技巧,需要的朋友可以參考下2018-01-01
后端如何接收格式為x-www-form-urlencoded的數(shù)據(jù)
x-www-form-urlencoded格式是一種常見的HTTP請(qǐng)求數(shù)據(jù)格式,它將請(qǐng)求參數(shù)編碼為鍵值對(duì)的形式,以便于傳輸和解析,下面這篇文章主要給大家介紹了關(guān)于后端如何接收格式為x-www-form-urlencoded的數(shù)據(jù),需要的朋友可以參考下2023-05-05
Struts2中ognl遍歷數(shù)組,list和map方法詳解
這篇文章主要介紹了Struts2中ognl遍歷數(shù)組,list和map方法詳解,需要的朋友可以參考下。2017-09-09
SpringBoot中@PostConstruct 注解的實(shí)現(xiàn)
在Spring Boot框架中,?@PostConstruct是一個(gè)非常有用的注解,它用于在依賴注入完成后執(zhí)行初始化方法,本文將介紹?@PostConstruct的基本概念、使用場(chǎng)景以及提供詳細(xì)的代碼示例,感興趣的可以了解一下2024-09-09
基于Java?利用Mybatis實(shí)現(xiàn)oracle批量插入及分頁(yè)查詢
這篇文章主要介紹了基于Java?利用Mybatis實(shí)現(xiàn)oracle批量插入及分頁(yè)查詢,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,需要的小伙伴可以參考一下2022-07-07
Java多線程CyclicBarrier的實(shí)現(xiàn)代碼
CyclicBarrier可以使一定數(shù)量的線程反復(fù)地在柵欄位置處匯集,本文通過實(shí)例代碼介紹下Java多線程CyclicBarrier的相關(guān)知識(shí),感興趣的朋友一起看看吧2022-02-02
Java和c語(yǔ)言隨機(jī)數(shù)Random代碼詳細(xì)
這篇文章主要介紹Java和c語(yǔ)言得隨機(jī)數(shù)Random,隨機(jī)數(shù)的用處在生活中比較少見,但是用處并不少,比如一些小游戲的制作等等。下面我們就一起來學(xué)習(xí)這篇關(guān)于Java和c隨機(jī)數(shù)Random得文章吧2021-10-10
java json 省市級(jí)聯(lián)實(shí)例代碼
這篇文章介紹了java json 省市級(jí)聯(lián)實(shí)例代碼,有需要的朋友可以參考一下2013-09-09

