手?jǐn)]一個 spring-boot-starter的全過程
我們使用 Spring Boot,基本上都是沉醉在它 Stater 的方便之中。Starter 為我們帶來了眾多的自動化配置,有了這些自動化配置,我們可以不費(fèi)吹灰之力就能搭建一個生產(chǎn)級開發(fā)環(huán)境,有的小伙伴會覺得這個 Starter 好神奇呀!其實 Starter 也都是 Spring + SpringMVC 中的基礎(chǔ)知識點實現(xiàn)的,接下來帶大家自己來擼一個 Starter ,慢慢揭開 Starter 的神秘面紗!
核心知識
其實 Starter 的核心就是條件注解 @Conditional ,當(dāng) classpath 下存在某一個 Class 時,某個配置才會生效。
定義自己的 Starter
所謂的 Starter ,其實就是一個普通的 Maven 項目,因此我們自定義 Starter ,需要首先創(chuàng)建一個普通的 Maven 項目,創(chuàng)建完成后,添加 Starter 的自動化配置類即可,如下:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> <version>2.1.8.RELEASE</version> </dependency>
配置完成后,我們首先創(chuàng)建一個 HelloProperties 類,用來接受 application.properties 中注入的值,如下:
@ConfigurationProperties(prefix = "mystarter")
public class HelloProperties {
private String name = DEFAULT_NAME;
private String msg = DEFAULT_MSG;
private static final String DEFAULT_NAME = "Antonio";
private static final String DEFAULT_MSG = "Java 工程師";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
這個配置類很好理解,將 application.properties 中配置的屬性值直接注入到這個實例中, @ConfigurationProperties 類型安全的屬性注入,即將 application.properties 文件中前綴為 mystarter 的屬性注入到這個類對應(yīng)的屬性上, 最后使用時候,application.properties 中的配置文件,大概如下:
mystarter.name=zhangsan mystarter.msg=java
配置完成 HelloProperties 后,接下來我們來定義一個 HelloService ,然后定義一個簡單的 say 方法, HelloService 的定義如下:
public class HelloService {
private String msg;
private String name;
public String sayHello() {
return name + " say " + msg + " !";
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
這個很簡單,沒啥好說的。接下來就是我們的重軸戲,自動配置類的定義,用了很多別人定義的自定義類之后,我們也來自己定義一個自定義類。先來看代碼吧,一會松哥再慢慢解釋:
@Configuration
@EnableConfigurationProperties(HelloProperties.class)
@ConditionalOnClass(HelloService.class)
public class HelloServiceAutoConfiguration {
@Autowired
HelloProperties helloProperties;
@Bean
HelloService helloService() {
HelloService helloService = new HelloService();
helloService.setName(helloProperties.getName());
helloService.setMsg(helloProperties.getMsg());
return helloService;
}
}
關(guān)于這一段自動配置,解釋如下:
- 首先
@Configuration注解表明這是一個配置類。 @EnableConfigurationProperties注解是使我們之前配置的@ConfigurationProperties生效,讓配置的屬性成功的進(jìn)入 Bean 中。@ConditionalOnClass表示當(dāng)項目當(dāng)前 classpath 下存在 HelloService 時,后面的配置才生效。- 自動配置類中首先注入 HelloProperties ,這個實例中含有我們在
application.properties中配置的相關(guān)數(shù)據(jù)。 - 提供一個 HelloService 的實例,將
HelloProperties中的值注入進(jìn)去。
做完這一步之后,我們的自動化配置類就算是完成了,接下來還需要一個 spring.factories 文件,那么這個文件是干嘛的呢?大家知道我們的 Spring Boot 項目的啟動類都有一個 @SpringBootApplication 注解,這個注解的定義如下:
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = {
@Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
@Filter(type = FilterType.CUSTOM,
classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {
}
大家看到這是一個組合注解,其中的一個組合項就是 @EnableAutoConfiguration,這個注解是干嘛的呢?@EnableAutoConfiguration 表示啟用 Spring 應(yīng)用程序上下文的自動配置,該注解會自動導(dǎo)入一個名為 AutoConfigurationImportSelector 的類,而這個類會去讀取一個名為 spring.factories 的文件, spring.factories 中則定義需要加載的自動化配置類,我們打開任意一個框架的 Starter ,都能看到它有一個 spring.factories 文件,例如 MyBatis 的 Starter 如下:

那么我們自定義 Starter 當(dāng)然也需要這樣一個文件,我們首先在 Maven 項目的 resources 目錄下創(chuàng)建一個名為 META-INF 的文件夾,然后在文件夾中創(chuàng)建一個名為 spring.factories 的文件,文件內(nèi)容如下:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.antonio.mystarter.HelloServiceAutoConfiguration
在這里指定我們的自動化配置類的路徑即可。如此之后我們的自動化配置類就算完成了。
本地安裝
如果在公司里,大伙可能需要將剛剛寫好的自動化配置類打包,然后上傳到 Maven 私服上,供其他同事下載使用,我這里就簡單一些,我就不上傳私服了,我將這個自動化配置類安裝到本地倉庫,然后在其他項目中使用即可。安裝方式很簡單,在 IntelliJ IDEA 中,點擊右邊的 Maven Project ,然后選擇 Lifecycle 中的 install ,雙擊即可,如下:

雙擊完成后,這個 Starter 就安裝到我們本地倉庫了,當(dāng)然小伙伴也可以使用 Maven 命令去安裝。
使用 Starter
接下來,我們來新建一個普通的 Spring Boot 工程,這個 Spring Boot 創(chuàng)建成功之后,加入我們自定義 Starter 的依賴,如下:
<dependency> <groupId>com.antonio</groupId> <artifactId>mystarter</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
此時我們引入了上面自定義的 Starter ,也即我們項目中現(xiàn)在有一個默認(rèn)的 HelloService 實例可以使用,而且關(guān)于這個實例的數(shù)據(jù),我們還可以在 application.properties 中進(jìn)行配置,如下:
mystarter.name=lisi mystarter.msg=java
配置完成后,方便起見,我這里直接在單元測試方法中注入 HelloSerivce 實例來使用,代碼如下:
@RunWith(SpringRunner.class)
@SpringBootTest
public class UsemystarterApplicationTests {
@Autowired
HelloService helloService;
@Test
public void contextLoads() {
System.out.println(helloService.sayHello());
}
}
執(zhí)行單元測試方法即可。
到此這篇關(guān)于手?jǐn)]一個 spring-boot-starter的文章就介紹到這了,更多相關(guān)spring-boot-starter內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用ShardingSphere-Proxy實現(xiàn)分表分庫
這篇文章介紹了使用ShardingSphere-Proxy實現(xiàn)分表分庫的方法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-02-02
mybatis Plus 多表聯(lián)合查詢的實現(xiàn)示例
這篇文章主要介紹了mybatis Plus 多表聯(lián)合查詢的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
springmvc用于方法鑒權(quán)的注解攔截器的解決方案代碼
這篇文章主要介紹了springmvc用于方法鑒權(quán)的注解攔截器的解決方案代碼,具有一定借鑒價值,需要的朋友可以參考下。2017-12-12
Java?ArrayList實現(xiàn)班級信息管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Java?ArrayList實現(xiàn)班級信息管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02

