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