SpringBoot如何實(shí)現(xiàn)starter原理詳解
1、Mybatis 自定義配置的分析
在我們自定義starter之前我們寫(xiě)了解一下Mybatis 是如何實(shí)現(xiàn)starter
在SpringBoot 引入的依賴(lài)如下:
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>
mybatis的maven 依賴(lài),主要涉及到的內(nèi)容,spring.factories、MybatisAutoConfiguration、MybatisProperties

我們來(lái)看一下 META-INF/spring.factories文件,這個(gè)文件是以Map 形式存放的。key是EnableAutoConfiguration類(lèi)的全類(lèi)名,
value是一個(gè)MybatisAutoConfiguration,這就是當(dāng)項(xiàng)目啟動(dòng)自動(dòng)配置的類(lèi)。
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration
MybatisAutoConfiguration

@Configuration //標(biāo)示是一個(gè)配置類(lèi)
@ConditionalOnClass({SqlSessionFactory.class, SqlSessionFactoryBean.class}) //表示當(dāng)SqlSessionFactory,SqlSessionFactoryBean存在這個(gè)配置類(lèi)才生效。
@EnableConfigurationProperties({MybatisProperties.class}):就是把 MybatisProperties加入到 IOC 容器中。
MybatisProperties

對(duì)于@ConfigurationProperties注解它的作用就是把全局配置文件中的值綁定到實(shí)體類(lèi)JavaBean上面(將配置文件中的值與MybatisProperties綁定起來(lái)),而@EnableConfigurationProperties主要是把以綁定值JavaBean加入到spring容器中。
分析完這些規(guī)則后,我們?cè)賮?lái)看看mybatis自定義的starter 的項(xiàng)目結(jié)構(gòu),主要是分為兩個(gè)項(xiàng)目(一個(gè)是空項(xiàng)目(mtbatis-spring-boot-starter),一個(gè)是具體的實(shí)現(xiàn)自定義配置的項(xiàng)目(mybatis-spring-boot-autoconfigure)),空項(xiàng)目只是引入自定義配置項(xiàng)目的依賴(lài),而實(shí)現(xiàn)映入的時(shí)候我們只需要映入空項(xiàng)(mtbatis-spring-boot-starter)即可。
到此我們已經(jīng)分析完mybatis 自定義的starter,下面我們自己來(lái)實(shí)現(xiàn)一個(gè)自定義的starter。
2、自定義starter的實(shí)現(xiàn)
項(xiàng)目結(jié)構(gòu)展示:

首先我們先定義一個(gè) zfauto-spring-boot-autoconfigure 工程
編寫(xiě)屬性類(lèi):添加 @ConfigurationProperties注解和前綴 zf.auto。之后我們就可以在 application.properties或application.yml 中 使用 zf.auto=指定參數(shù)了,由于篇幅的原因省略setter getter方法,實(shí)際是需要的,不然無(wú)法注入;
@ConfigurationProperties(prefix = "zf.auto")
public class HelloProperties {
private String prefix;
private String suffix;
}
編寫(xiě)配置類(lèi):加入@Configuration注解,@ConditionalOnWebApplication是web 應(yīng)用配置類(lèi)才起作用,以及 @EnableConfigurationProperties(HelloProperties.class) 注解,將屬性注入到 IOC 容器中。
@Configuration
@ConditionalOnWebApplication
@EnableConfigurationProperties(HelloProperties.class)
public class HelloServiceAutoConfiguration {
@Autowired
HelloProperties helloProperties;
@Bean
public HelloService helloService(){
HelloService helloService=new HelloService();
helloService.setHelloProperties(helloProperties);
return helloService;
}
}
編寫(xiě) spring.factories 文件:在resources路徑下面創(chuàng)建META-INF,文件夾,然后創(chuàng)建spring.factories文件
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.zfauto.starter.HelloServiceAutoConfiguration
然后我們?cè)趧?chuàng)建一個(gè)空項(xiàng)目(zfauto-spring-boot-starter),在這個(gè)項(xiàng)目中我們引入zfauto-spring-boot-autoconfigure依賴(lài)
<dependency>
<groupId>com.zfauto.starter</groupId>
<artifactId>zfauto-spring-boot-autoconfigure</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
HelloService 實(shí)現(xiàn)的功能,省略setter,getter的方法(實(shí)際需要)
public class HelloService {
HelloProperties helloProperties;
public String sayHello(String name){
return helloProperties.getPrefix()+ ","+name+","+helloProperties.getSuffix();
}
}
最后我們 分別將項(xiàng)目打包,由于zfauto-spring-boot-starter是依賴(lài)于zfauto-spring-boot-autoconfigure,所以我們先對(duì)zfauto-spring-boot-autoconfigure進(jìn)行打包,然后通過(guò) mvn install 打到本地倉(cāng)庫(kù)(如何打包見(jiàn)下圖)。

到此我們自定義的類(lèi)實(shí)現(xiàn)。那我們來(lái)測(cè)試一下,這個(gè)和我們引入其他的starter一樣了。
創(chuàng)建項(xiàng)目zfauto-spring-boot-starter-test ,引入自定義starter的依賴(lài)。
<dependency>
<groupId>com.zfauto.starter</groupId>
<artifactId>zfauto-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
application.properties中的配置如下
zf.auto.prefix=hello
zf.auto.suffix=123
具體的測(cè)試類(lèi)
@RestController
public class HelloController {
@Autowired
HelloService helloService;
@RequestMapping("/sayHello")
public String sayHello(){
return helloService.sayHello("小福子");
}
}
項(xiàng)目訪問(wèn)路徑:http://localhost:8080/sayHello

好了 ,本文就說(shuō)到這里,本文相關(guān)案例我已經(jīng)上傳到 碼云 ,小伙伴們可以自行下載:https://gitee.com/xiaofuzi123/springboot-learning-example
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
升級(jí)dubbo2.7.4.1版本平滑遷移到注冊(cè)中心nacos
這篇文章主要為大家介紹了2.7.4.1的dubbo平滑遷移到注冊(cè)中心nacos的兩種版本升級(jí)方案,以及為什要升級(jí),有需要的朋友可以借鑒參考下,希望能夠有所幫助2022-02-02
MyBatis-Plus多表聯(lián)合查詢(xún)并且分頁(yè)(3表聯(lián)合)
這篇文章主要介紹了MyBatis-Plus多表聯(lián)合查詢(xún)并且分頁(yè)(3表聯(lián)合),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
java實(shí)現(xiàn)雙層圣誕樹(shù)加修飾代碼示例
大家好,本篇文章主要講的是java實(shí)現(xiàn)雙層圣誕樹(shù)加修飾代碼示例,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話(huà)記得收藏一下,方便下次瀏覽2021-12-12
Java微服務(wù)Filter過(guò)濾器集成Sentinel實(shí)現(xiàn)網(wǎng)關(guān)限流過(guò)程詳解
這篇文章主要介紹了Java微服務(wù)Filter過(guò)濾器集成Sentinel實(shí)現(xiàn)網(wǎng)關(guān)限流過(guò)程,首先Sentinel規(guī)則的存儲(chǔ)默認(rèn)是存儲(chǔ)在內(nèi)存的,應(yīng)用重啟之后規(guī)則會(huì)丟失。因此我們通過(guò)配置中心Nacos保存規(guī)則,然后通過(guò)定時(shí)拉取Nacos數(shù)據(jù)來(lái)獲取規(guī)則配置,可以做到動(dòng)態(tài)實(shí)時(shí)的刷新規(guī)則2023-02-02
Spring?Cloud中Sentinel的兩種限流模式介紹
如何使用Sentinel做流量控制呢?這篇文章就來(lái)為大家詳細(xì)介紹了Spring?Cloud中Sentinel的兩種限流模式,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-05-05
jdk17+springboot使用webservice的踩坑實(shí)戰(zhàn)記錄
這篇文章主要給大家介紹了關(guān)于jdk17+springboot使用webservice踩坑的相關(guān)資料,網(wǎng)上很多教程是基于jdk8的,所以很多在17上面跑不起來(lái),折騰兩天,直接給答案,需要的朋友可以參考下2024-01-01

