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

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

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

