SpringBoot詳解自定義Stater的應(yīng)用
1、SpringBoot starter機(jī)制
SpringBoot由眾多Starter組成(一系列的自動化配置的starter插件),SpringBoot之所以流行,也是因為starter。starter是SpringBoot非常重要的一部分,可以理解為一個可拔插式的插件,正是這些starter使得使用某個功能的開發(fā)者不需要關(guān)注各種依賴庫的處理,不需要具體的配置信息,由Spring Boot自動通過classpath路徑下的類發(fā)現(xiàn)需要的Bean,并織入相應(yīng)的Bean。
例如,你想使用Reids插件,那么可以使用spring-boot-starter-redis;如果想使用MongoDB,可以使用spring-boot-starter-data-mongodb
2、為什么要自定義starter
開發(fā)過程中,經(jīng)常會有一些獨立于業(yè)務(wù)之外的配置模塊。如果我們將這些可獨立于業(yè)務(wù)代碼之外的功能配置模塊封裝成一個個starter,復(fù)用的時候只需要將其在pom中引用依賴即可,SpringBoot為我們完成自動裝配
3、自定義starter的命名規(guī)則
SpringBoot提供的starter以spring-boot-starter-xxx 的方式命名的。官方建議自定義的starter使用xxx-spring-boot-starter 命名規(guī)則。以區(qū)分SpringBoot生態(tài)提供的starter
整個過程分為兩部分:
- 自定義starter
- 使用starter
首先,先完成自定義starter
(1)新建maven jar工程,工程名為zdy-spring-boot-starter,導(dǎo)入依賴:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
</dependencies>(2)編寫javaBean
package com.lagou;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
@EnableConfigurationProperties(SimpleBean.class) // 開啟@ConfigurationProperties
@ConfigurationProperties(prefix = "simplebean")
public class SimpleBean {
private int id;
private String name;
@Override
public String toString() {
return "SimpleBean{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}(3)編寫配置類MyAutoConfiguration
package com.lagou.config;
import com.lagou.SimpleBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConditionalOnClass// 注解:當(dāng)類路徑classpath下有指定類的情況,就會進(jìn)行自動配置
public class MyAutoConfiguration {
static {
System.out.println("MyAutoConfiguration init....");
}
@Bean
public SimpleBean simpleBean() {
return new SimpleBean();
}
}(4)resources下創(chuàng)建/META-INF/spring.factories
注意:META-INF是自己手動創(chuàng)建的目錄,spring.factories也是手動創(chuàng)建的文件,在該文件中配置自己的自動配置類

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.lagou.config.MyAutoConfiguration
4、使用自定義starter
(1)導(dǎo)入自定義starter的依賴
<dependency>
<groupId>com.lagou</groupId>
<artifactId>zdy-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>(2)在全局配置文件中配置屬性值
simplebean.id=1
simplebean.name=自定義starter
(3)編寫測試方法
//測試自定義starter
@Autowired
private SimpleBean simpleBean;
@Test
public void zdyStarterTest(){
System.out.println(simpleBean);
}到此這篇關(guān)于SpringBoot詳解自定義Stater的應(yīng)用的文章就介紹到這了,更多相關(guān)SpringBoot Stater內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
三分鐘讀懂mybatis中resultMap和resultType區(qū)別
這篇文章主要給大家介紹了mybatis中resultMap和resultType區(qū)別的相關(guān)資料,resultType和resultMap都是mybatis進(jìn)行數(shù)據(jù)庫連接操作處理返回結(jié)果的,需要的朋友可以參考下2023-07-07
使用Spring Cloud Feign作為HTTP客戶端調(diào)用遠(yuǎn)程HTTP服務(wù)的方法(推薦)
在Spring Cloud中使用Feign, 我們可以做到使用HTTP請求遠(yuǎn)程服務(wù)時能與調(diào)用本地方法一樣的編碼體驗,開發(fā)者完全感知不到這是遠(yuǎn)程方法,更感知不到這是個HTTP請求,具體內(nèi)容詳情大家參考下本文2018-01-01
Retrofit+Rxjava實現(xiàn)文件上傳和下載功能
這篇文章主要介紹了Retrofit+Rxjava實現(xiàn)文件上傳和下載功能,文中提到了單文件上傳和多文件上傳及相關(guān)參數(shù)的請求,需要的朋友參考下吧2017-11-11
SpringBoot?整合MyBatis+MyBatis-Plus+MyBatisX插件使用
本文主要介紹了SpringBoot?整合MyBatis+MyBatis-Plus+MyBatisX插件使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-04-04
教你用Springboot實現(xiàn)攔截器獲取header內(nèi)容
項目中遇到一個需求,對接上游系統(tǒng)是涉及到需要增加請求頭,請求頭的信息是動態(tài)獲取的,需要動態(tài)從下游拿到之后轉(zhuǎn)給上游,文中非常詳細(xì)的介紹了該需求的實現(xiàn),需要的朋友可以參考下2021-05-05
使用shardingsphere對SQLServer坑的解決
本文主要介紹了使用shardingsphere對SQLServer坑的解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-03-03

