springboot中的starter及自定義方法詳解
1:官方提供的starter
在spring-boot-autocongure包中定義了官方提供的一百多個(gè)starter,如下:

2:框架是如何定義starter的?
因?yàn)閟pringboot的普及度逐步提高,一些沒(méi)有被官方實(shí)現(xiàn)提供為starter的框架,也會(huì)自己實(shí)現(xiàn)一個(gè)starter供用戶(hù)使用,這里以shardingsphere ,clone之后使用操作git checkout -b 5.0.0-alpha-local 5.0.0-alpha創(chuàng)建分支,然后在如下目錄查看starter配置:
$ pwd /d/test/sharding-sphere/shardingsphere-jdbc/shardingsphere-jdbc-spring/shardingsphere-jdbc-core-spring/shardingsphere-jdbc-core-spring-boot-starter
截圖如下:

spring.provider內(nèi)容如下:
provides: shardingsphere-jdbc-spring-boot-starter
additional-spring-configuration-metadata.json內(nèi)容如下:
{
"groups": [
{
"name": "spring.shardingsphere.datasource",
"type": "org.apache.shardingsphere.spring.boot.SpringBootConfiguration"
},
...
}
其中比較重要的是spring.factories和自動(dòng)配置類(lèi)SpringBootConfiguration,spring.factories如下:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ org.apache.shardingsphere.spring.boot.SpringBootConfiguration
其中只定義了自動(dòng)配置類(lèi),自動(dòng)配置類(lèi)如下:
@Configuration // 代表是一個(gè)Java config類(lèi)
@ComponentScan("org.apache.shardingsphere.spring.boot.converter") // 配置掃描路徑,會(huì)掃描并注冊(cè)相關(guān)spring bean
@EnableConfigurationProperties(SpringBootPropertiesConfiguration.class) // 啟用屬性類(lèi),封裝了外部配置文件信息
@ConditionalOnProperty(prefix = "spring.shardingsphere", name = "enabled", havingValue = "true", matchIfMissing = true) // 當(dāng)spring.shardingsphere.enabled 配置項(xiàng)等于true時(shí),加載該自動(dòng)配置類(lèi)
@AutoConfigureBefore(DataSourceAutoConfiguration.class) // 在DataSourceAutoConfiguration自動(dòng)配置類(lèi)之前配置
@RequiredArgsConstructor // lombok注解
public class SpringBootConfiguration implements EnvironmentAware {
// 屬性類(lèi),會(huì)自動(dòng)注入進(jìn)來(lái)
private final SpringBootPropertiesConfiguration props;
private final Map<String, DataSource> dataSourceMap = new LinkedHashMap<>();
// 核心,創(chuàng)建shardingsphere
@Bean
@Autowired(required = false)
public DataSource shardingSphereDataSource(final ObjectProvider<List<RuleConfiguration>> rules) throws SQLException {
Collection<RuleConfiguration> ruleConfigurations = Optional.ofNullable(rules.getIfAvailable()).orElse(Collections.emptyList());
return ShardingSphereDataSourceFactory.createDataSource(dataSourceMap, ruleConfigurations, props.getProps());
}
// 創(chuàng)建spring bean ShardingTransactionTypeScanner
@Bean
public ShardingTransactionTypeScanner shardingTransactionTypeScanner() {
return new ShardingTransactionTypeScanner();
}
// 感知上下文對(duì)象,因?yàn)樽詣?dòng)配置類(lèi)本身也是一個(gè)spring bean,所以這里可以這樣子用
@Override
public final void setEnvironment(final Environment environment) {
dataSourceMap.putAll(DataSourceMapSetter.getDataSourceMap(environment));
}
}
使用也比較簡(jiǎn)單,和引入普通的jar包相同,在需要的項(xiàng)目里引入其GAV即可,最終springboot會(huì)掃描到spring.facatories,并加載其中的組件,當(dāng)然最重要的自然是自動(dòng)配置類(lèi)了,接下來(lái)我們仿照shardingsphere 的starter來(lái)自定義一個(gè)starter。
3:自定義starter
3.1:自定義starter
首先我們來(lái)定義屬性類(lèi),如下:
@ConfigurationProperties(prefix = "info")
@Getter
@Setter
public class HelloProperties {
// private Properties prop = new Properties();
private String name;
}
即接收的配置的前綴是info,這里的name屬性配置中就可能是info.name=xxxx,接著我們來(lái)定義一個(gè)我們的starter能夠提供的類(lèi)Spokesman,該類(lèi)的功能就是會(huì)說(shuō)話(huà),因此,使用了我們starter就能擁有一個(gè)會(huì)spoke的man,如下:
@AllArgsConstructor
public class Spokesman {
private String name;
public String speak() {
System.out.println("你好,向大家介紹:" + name);
return name;
}
}
接著就可以定義最重要的自動(dòng)配置類(lèi)了,如下:
@Configuration
@EnableConfigurationProperties(HelloProperties.class)
@ConditionalOnExpression // 默認(rèn)為true,即默認(rèn)加載
@AllArgsConstructor
public class HelloAutoConfiguration implements InitializingBean {
private final HelloProperties props;
// 創(chuàng)建 Spokesman 的 spring bean
@Bean
public Spokesman createSpokesman() {
return new Spokesman(props.getName());
}
// 僅僅為了打印props,沒(méi)有其他特殊用途
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("props isss: " + props.getName());
}
}
但此時(shí),我們的自動(dòng)配置類(lèi)還不能加入到springboot的體系中,還需要在META-INF下創(chuàng)建spring.factories文件,如下:

3.2:使用自定義starter
starter其實(shí)也就是一個(gè)普通的maven依賴(lài),所以我們想要使用的話(huà),首先需要引入其對(duì)應(yīng)的GAV,這里如下:
<dependency>
<groupId>dongshi.daddy</groupId>
<artifactId>hello-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
接著我們提供starter需要的配置信息,如下:
info: name: 甜甜的葡萄干
接著我們就可以獲取starter提供的SpokeMan來(lái)spoke了,如下:
@Resource
private Spokesman spokesman;
@RequestMapping("/starter")
@ResponseBody
public String starter() {
System.out.println("starter ...");
return spokesman.speak();
}
運(yùn)行main,如果是在console看到如下輸出,則說(shuō)明starter已經(jīng)使用成功了:
2023-07-03 21:01:56.508 INFO 3011 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2355 ms
props isss: 甜甜的葡萄干
...
接著我們就可以訪(fǎng)問(wèn)//localhost:8899/my/starter了:

到此這篇關(guān)于springboot中的starter及自定義方法詳解的文章就介紹到這了,更多相關(guān)springboot中的starter內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Maven的生命周期與自定義插件實(shí)現(xiàn)方法
Maven的生命周期就是對(duì)所有的構(gòu)建過(guò)程進(jìn)行抽象和統(tǒng)一。包含了項(xiàng)目的清理、初始化、編譯、測(cè)試、打包、集成測(cè)試、驗(yàn)證、部署和站點(diǎn)生成等幾乎所有的構(gòu)建步驟2022-12-12
java的Array,List和byte[],String相互轉(zhuǎn)換的方法你了解嘛
這篇文章主要為大家詳細(xì)介紹了java的Array,List和byte[],String相互轉(zhuǎn)換的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2022-02-02
新手也能看懂的SpringBoot異步編程指南(簡(jiǎn)單易懂)
這篇文章主要介紹了新手也能看懂的SpringBoot異步編程指南(簡(jiǎn)單易懂),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
Java ScheduledExecutorService的具體使用
ScheduledExecutorService有線(xiàn)程池的特性,也可以實(shí)現(xiàn)任務(wù)循環(huán)執(zhí)行,本文主要介紹了Java ScheduledExecutorService的具體使用,具有一定的參考價(jià)值,感興趣的可以了解一下2023-05-05
將對(duì)象轉(zhuǎn)化為字符串的java實(shí)例
這篇文章主要介紹了將對(duì)象轉(zhuǎn)化為字符串的java實(shí)例,有需要的朋友可以參考一下2013-12-12
解決springboot?druid數(shù)據(jù)庫(kù)連接池連接失敗后一直重連問(wèn)題
這篇文章主要介紹了解決springboot?druid數(shù)據(jù)庫(kù)連接池連接失敗后一直重連問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11
java中駝峰與下劃線(xiàn)的寫(xiě)法互轉(zhuǎn)
這篇文章主要介紹了java中駝峰與下橫線(xiàn)的寫(xiě)法互轉(zhuǎn)方法,文中先是進(jìn)行了簡(jiǎn)單的介紹,之后跟大家分享了一個(gè)自己編寫(xiě)的工具類(lèi)的示例代碼,有需要的朋友可以參考借鑒,下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-01-01

