SpringBoot自動(dòng)配置之自定義starter的實(shí)現(xiàn)代碼
前言:前面已經(jīng)介紹了自動(dòng)配置的很多原理,現(xiàn)在我們著手自己定義一個(gè)starter。
需求:自定義redis-starter,要求當(dāng)導(dǎo)入redis坐標(biāo)后,SpringBoot自動(dòng)創(chuàng)建Jedis的Bean。正式開(kāi)始之前,我們可以查看Mybatis的起步依賴是如果實(shí)現(xiàn)自動(dòng)配置的。我這里就省略了,大家根據(jù)之前的分析文章,自己看源碼即可。
一、先創(chuàng)建一個(gè)SpringBoot工程redis-spring-boot-autoconfigure,該工程中添加jedis依賴,并且創(chuàng)建一個(gè)自動(dòng)配置類RedisAutoConfigure,實(shí)現(xiàn)jedis實(shí)例(bean)注入到IOC容器中。
/**
* @description:自定義自動(dòng)配置類:將Jedis注入到IOC容器中
* @date: 2020/10/10 15:08
* @author: winson
*/
@Configuration
@EnableConfigurationProperties(RedisProperties.class)
public class RedisAutoConfigure {
/**
* 提供jedis的bean
* @param redisProperties
* @return
*/
@Bean
public Jedis jedis(RedisProperties redisProperties) {
return new Jedis(redisProperties.getHost(), redisProperties.getPort());
}
}
考慮到redis的有兩個(gè)參數(shù)(host、port)必須是可以動(dòng)態(tài)賦值的,所以我們自定義一個(gè)屬性配置類RedisProperties,該屬性配置類從配置文件獲取屬性值,并設(shè)置host、port屬性的默認(rèn)值,如下:
/**
* @description:自定義屬性配置類
* @date: 2020/10/10 15:11
* @author: winson
*/
@ConfigurationProperties(prefix = "redis")
public class RedisProperties {
private String host = "127.0.0.1";
private int port = 6379;
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
}
@EnableConfigurationProperties注解的作用:由于自動(dòng)配置類RedisAutoConfigure中,注冊(cè)jedis的bean中參數(shù)需要使用到RedisProperties的bean,但無(wú)法獲取到RedisProperties的bean,所以我們可以通過(guò)這個(gè)注解,手動(dòng)的將該bean注入到IOC容器中。
在resources目錄下按照SpringBoot自動(dòng)配置目錄、文件名稱的規(guī)則,如下圖,新建一個(gè)自己的spring.factories文件

spring.factories文件內(nèi)容:就是將自定義的自動(dòng)配置類聲明為名稱為org.springframework.boot.autoconfigure.EnableAutoConfiguration的值,“\”的作用:換行,如果我們有多個(gè)自定義的自動(dòng)配置類,這里可以用逗號(hào)分隔。
# Auto Configure org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.winson.jedis.config.RedisAutoConfigure
二、再創(chuàng)建一個(gè)SpringBoot工程redis-spring-boot-starter,引入redis-spring-boot-autoconfigure工程坐標(biāo)。
<!--引入自定義的redis的starter--> <dependency> <groupId>com.winson</groupId> <artifactId>redis-spring-boot-autoconfigure</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency>
三、在測(cè)試類中引入自定義的starter,并修改引導(dǎo)類,做測(cè)試
<!--引入自定義的起步依賴--> <dependency> <groupId>com.winson</groupId> <artifactId>redis-spring-boot-starter</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency>
//引導(dǎo)類
@SpringBootApplication
public class SpringbootEnableApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(SpringbootEnableApplication.class, args);
Jedis jedis = context.getBean(Jedis.class);
System.out.println(jedis);
}
}
四、啟動(dòng)程序,測(cè)試結(jié)果,自定義stater成功。

下面進(jìn)行redis測(cè)試
一、首先啟動(dòng)本機(jī)的redis服務(wù)

二、根據(jù)測(cè)試,修改引導(dǎo)類,向redis中存儲(chǔ)數(shù)據(jù),并取值
@SpringBootApplication
public class SpringbootEnableApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(SpringbootEnableApplication.class, args);
Jedis jedis = context.getBean(Jedis.class);
jedis.set("username", "dufu");
String username = jedis.get("username");
System.out.println("username=" + username);
}
}
三、啟動(dòng)程序,測(cè)試結(jié)果,也成功。

下面測(cè)試修改redis連接信息,測(cè)試結(jié)果
一、測(cè)試類中添加redis的配置信息(根據(jù)屬性配置類 RedisProperties 的設(shè)置進(jìn)行定義),將port的連接端口,故意寫(xiě)錯(cuò),如下:
redis: host: 127.0.0.1 port: 6666
二、啟動(dòng)程序,測(cè)試結(jié)果:發(fā)現(xiàn)無(wú)法再連接到redis服務(wù)器了,說(shuō)明我們?cè)O(shè)置的屬性配置類RedisProperties 是生效的

三、如果我們將連接信息修改正確,再啟動(dòng),也是沒(méi)有問(wèn)題的

最后,我們可以使用Condition將我們自定義的自動(dòng)配置類完善一下,使用@ConditionalOnClass(Jedis.class)與@ConditionalOnMissingBean(name = "jedis"),具體的釋義,我就不解釋。
/**
* @description:自定義自動(dòng)配置類:將Jedis注入到IOC容器中
* @date: 2020/10/10 15:08
* @author: winson
*/
@Configuration
@EnableConfigurationProperties(RedisProperties.class)
@ConditionalOnClass(Jedis.class)
public class RedisAutoConfigure {
/**
* 提供jedis的bean
* @param redisProperties
* @return
*/
@Bean
@ConditionalOnMissingBean(name = "jedis")
public Jedis jedis(RedisProperties redisProperties) {
return new Jedis(redisProperties.getHost(), redisProperties.getPort());
}
}
小結(jié):如此,我們并完成了自定義starter的示例
到此這篇關(guān)于SpringBoot自動(dòng)配置之自定義starter的實(shí)現(xiàn)代碼的文章就介紹到這了,更多相關(guān)SpringBoot自定義starter內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- spring boot 自定義starter的實(shí)現(xiàn)教程
- springboot自定義Starter的具體流程
- Spring boot創(chuàng)建自定義starter的完整步驟
- spring boot微服務(wù)自定義starter原理詳解
- springboot自定義starter實(shí)現(xiàn)過(guò)程圖解
- springboot自定義redis-starter的實(shí)現(xiàn)
- 使用SpringBoot自定義starter的完整步驟
- Java SpringBoot自定義starter詳解
- SpringBoot如何自定義starter
- SpringBoot自定義start詳細(xì)圖文教程
相關(guān)文章
教你用Java Swing做一個(gè)定時(shí)提醒工具
今天給大家?guī)?lái)的是Java的相關(guān)知識(shí),文章圍繞著如何用Java做一個(gè)定時(shí)提醒工具展開(kāi),文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下2021-06-06
idea maven 項(xiàng)目src下的配置文件沒(méi)有同步至target的解決操作
這篇文章主要介紹了idea maven 項(xiàng)目src下的配置文件沒(méi)有同步至target的解決操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08
@Scheduled 如何讀取動(dòng)態(tài)配置文件
這篇文章主要介紹了@Scheduled 如何讀取動(dòng)態(tài)配置文件的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
springboot2.0+elasticsearch5.5+rabbitmq搭建搜索服務(wù)的坑
這篇文章主要介紹了springboot2.0+elasticsearch5.5+rabbitmq搭建搜索服務(wù)的坑,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-06-06
Java中的ArrayList.trimToSize()方法詳解
這篇文章主要介紹了Java中的ArrayList.trimToSize()方法詳解,前幾天看了Java?ArrayList,沒(méi)有明白trimToSize()這個(gè)方法是什么意思,所以看了一下源碼并且debug一下自己的一個(gè)例子,明白了其中的含義,需要的朋友可以參考下2023-11-11
Jedis零基礎(chǔ)入門及操作Redis中的數(shù)據(jù)結(jié)構(gòu)詳解
Jedis 的 API 方法跟 Redis 的命令基本上完全一致,熟悉 Redis 的操作命令,自然就很容易使用 Jedis,因此官方也推薦 Java 使用 Jedis 來(lái)連接和操作 Redis2022-09-09
Spring boot工具類靜態(tài)屬性注入及多環(huán)境配置詳解
這篇文章主要為大家詳細(xì)介紹了Spring boot工具類靜態(tài)屬性注入,及多環(huán)境配置詳解,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04

