SpringBoot中的自定義starter
自定義starter
所謂的 starter ,其實(shí)就是一個(gè)普通的 Maven 項(xiàng)目。
1、首先創(chuàng)建一個(gè)普通的 Maven 項(xiàng)目
引入自動(dòng)化配置依賴:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> <version>2.1.8.RELEASE</version> <!-- 表明不傳遞spring-boot-autoconfigure依賴 --> <optional>true</optional> </dependency>
注意:在自定義的starter 的pom中,將spring-boot-autoconfigure的maven依賴聲明為<optional>true</optional>,表明自定義的starter的jar包不會(huì)傳遞spring-boot-autoconfigure依賴;否則會(huì)將spring-boot-autoconfigure版本固定,導(dǎo)致引用自定義starter的應(yīng)用出現(xiàn)版本沖突問(wèn)題。
starter命名模式 --> ${module}-spring-boot-starter;
例如:
<groupId>com.hello</groupId> <artifactId>helloService-spring-boot-starter</artifactId> <version>0.0.1-SNAPSHOT</version>
2、創(chuàng)建一個(gè)配置類
創(chuàng)建一個(gè)配置類 HelloProperties 來(lái)接受 application.properties 中注入的值:
@ConfigurationProperties(prefix = "javaboy") public class HelloProperties { privatestaticfinal String DEFAULT_NAME = "默認(rèn)名"; privatestaticfinal String DEFAULT_MSG = "默認(rèn)消息"; private String name = DEFAULT_NAME; private String msg = DEFAULT_MSG; 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; } }
3、創(chuàng)建一個(gè)服務(wù)類 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; } }
4、自定義自動(dòng)配置類
SpringBoot自動(dòng)裝配的命名規(guī)則:
- 自動(dòng)裝配類應(yīng)命名為:XxxAutoConfiguration;
- 自動(dòng)裝配package命名模式: ${root-package}.autoconfigure.${module-package},比如:
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration
@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; } }
注解 @EnableConfigurationProperties 使我們之前配置的 @ConfigurationProperties 生效,讓配置的屬性注入到 Bean 中。
注解 @ConditionalOnClass 表示當(dāng)項(xiàng)目的 classpath 下存在 HelloService 時(shí),后面的配置才生效。
5、創(chuàng)建 spring.factories 文件
在resources目錄下新建 META-INF/spring.factories 文件:
org.springframework.boot.autoconfigure.EnableAutoConfiguration = \ com.hello.autoconfigure.HelloServiceAutoConfiguration
這里的key是固定的,value是我們的自動(dòng)配置類的全路徑。
SpringBoot 項(xiàng)目啟動(dòng)時(shí),會(huì)做自動(dòng)配置,會(huì)去掃描所有jar包中的 META-INF/spring.factories 配置文件。
至此,自定義的starter已完成。將這個(gè)自動(dòng)化配置類的項(xiàng)目安裝到maven倉(cāng)庫(kù)中,然后在其他項(xiàng)目中引入依賴就可以使用。
新建一個(gè)普通的 Spring Boot 工程,并加入我們自定義的 starter 的依賴:
<dependency> <groupId>com.hello</groupId> <artifactId>helloService-spring-boot-starter</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency>
此時(shí)我們項(xiàng)目中已有一個(gè) helloService 實(shí)例可以使用,而且我們還可以配置它的屬性數(shù)據(jù),例如,在 application.properties 中配置:
javaboy.name=我的名字 javaboy.msg=提示消息
我們做個(gè)單元測(cè)試:
@RunWith(SpringRunner.class) @SpringBootTest public class MystarterApplicationTest { @Autowired private HelloService helloService; @Test public void contextLoads() { System.out.println(helloService.sayHello()); } }
控制臺(tái)會(huì)打印:
我的名字 say 提示消息 !
到此這篇關(guān)于SpringBoot中的自定義starter的文章就介紹到這了,更多相關(guān)自定義starter內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot中創(chuàng)建的AOP不生效的原因及解決
這篇文章主要介紹了SpringBoot中創(chuàng)建的AOP不生效的原因及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11Java中Spring Boot+Socket實(shí)現(xiàn)與html頁(yè)面的長(zhǎng)連接實(shí)例詳解
這篇文章主要介紹了Java中Spring Boot+Socket實(shí)現(xiàn)與html頁(yè)面的長(zhǎng)連接實(shí)例詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07Java?LinkedList實(shí)現(xiàn)班級(jí)信息管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Java?LinkedList實(shí)現(xiàn)班級(jí)信息管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02Java實(shí)現(xiàn)銀行賬戶管理子系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)銀行賬戶管理子系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05Shiro+Redis實(shí)現(xiàn)登錄次數(shù)凍結(jié)的示例
這篇文章主要介紹了Shiro+Redis實(shí)現(xiàn)登錄次數(shù)凍結(jié),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12解決MyBatis中為類配置別名,列名與屬性名不對(duì)應(yīng)的問(wèn)題
這篇文章主要介紹了解決MyBatis中為類配置別名,列名與屬性名不對(duì)應(yīng)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-11-11