SpringBoot手寫自定義starter源碼
1,前言
(1)SpringBoot的優(yōu)點
SpringBoot是新一代流行的Spring應(yīng)用開發(fā)框架,它具有更多的優(yōu)點:
- 創(chuàng)建獨立的Spring應(yīng)用
- 內(nèi)嵌Tomcat、Jetty或Undertow(無需部署war包)
- 提供自用的starter來簡化構(gòu)建配置
- 提供指標監(jiān)控、運行狀況檢查和外部化配置
- 沒有代碼生成,也不需要XML配置(約定大于配置)
(2)SpringBoot-starter的作用
SpringBoot擁有很多方便使用的starter(Spring提供的starter命名規(guī)范spring-boot-starter-xxx.jar,第三方提供的starter命名規(guī)范xxx-spring-boot-starter.jar),比如spring-boot-starter-log4j、mybatis-spring-boot-starter.jar等,各自都代表了一個相對完整的功能模塊。
SpringBoot-starter是一個集成接合器,完成兩件事:
- 引入模塊所需的相關(guān)jar包
- 自動配置各自模塊所需的屬性
2,為什么要自定義starter?
在我們的日常開發(fā)工作中,經(jīng)常會有一些獨立于業(yè)務(wù)之外的配置模塊,我們經(jīng)常將其放到一個特定的包下,然后如果另一個工程需要復(fù)用這塊功能的時候,需要將代碼硬拷貝到另一個工程,重新集成一遍,麻煩至極。如果我們將這些可獨立于業(yè)務(wù)代碼之外的功配置模塊封裝成一個個starter,復(fù)用的時候只需要將其在pom中引用依賴即可,SpringBoot為我們完成自動裝配,簡直不要太爽。
3,自定義starter的實現(xiàn)方式
首先新建一個maven工程,切記,只是一個maven工程而已,并非springboot工程!
(1)引入自動配置依賴和自動提示依賴
自動提示就是在properties或yml配置文件中輸入時自動提示!
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> <version>2.0.3.RELEASE</version> </dependency> <!--輸入properties或yml會自動提示--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <version>2.1.3.RELEASE</version> </dependency>
(2)寫一個TestBean,裝載信息
public class TestBean { private String msg; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } @Override public String toString() { return "TestBean{" + "msg='" + msg + '\'' + '}'; } }
(3)寫一個Properties類
該類上面的注解@ConfigurationProperties(prefix = "hello")是獲取配置文件的配置前綴為hello的值,然后賦值給msg變量
@ConfigurationProperties(prefix = "hello") public class HelloServiceProperties { private static final String MSG="hello world"; private String msg=MSG; public static String getMSG() { return MSG; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } }
(4)寫一個自動配置類 MyAutoConfiguration
引用定義好的配置信息;在AutoConfiguration中實現(xiàn)所有starter應(yīng)該完成的操作,并且把這個類加入spring.factories配置文件中進行聲明
@Configuration @ConditionalOnClass({TestBean.class})//判斷當前classpath下是否存在指定類,若是則將當前的配置裝載入spring容器 @EnableConfigurationProperties(HelloServiceProperties.class)//激活自動配置(指定文件中的配置) public class MyAutoConfiguration { @Autowired HelloServiceProperties helloServiceProperties;//注入測試的配置信息類 @Bean @ConditionalOnMissingBean(TestBean.class) //當前上下文中沒有TestBean實例時創(chuàng)建實例 public TestBean getTestService(){ TestBean testBean=new TestBean(); testBean.setMsg(helloServiceProperties.getMsg()); return testBean; } }
完成上面的操作還不夠,最重要的一步是接下來的一步 在resources文件夾下新建文件夾META-INF/spring.factories,將上面的自定義配置類MyAutoConfiguration的全路徑名+類名配置到該文件中(遵循spring.factories的格式),這樣隨著項目的啟動就可以實現(xiàn)自動裝配! 文件內(nèi)容
# Auto Configure org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.ftx.tomcat.bean.MyAutoConfiguration
(5)把本maven項目打包,上傳到maven私服
使用maven構(gòu)建工具打包自定義starter項目
mvn clean install
然后上傳到maven私服上,再新建一個springboot項目,把私服和starter項目的maven依賴配置到pom文件中
<dependency> <groupId>com.ftx.starter</groupId> <artifactId>write-starter</artifactId> <version>1.3</version> </dependency> <repositories> <repository> <id>nexus</id> <name>nexus</name> <url>http://nexus.tiger2.cn/nexus/content/groups/public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories>
可以找到對應(yīng)的jar包
(6)測試
在新建的springboot項目中寫一個TestController進行測試 注入TestBean,并使用TestBean
@RestController @RequestMapping("/test") public class TestController { @Autowired TestBean testBean; @RequestMapping("/user") public String user(){ User user=new User(); user.setName(testBean.getMsg()); return user.toString(); } }
然后啟動項目,訪問//localhost:8080/test/user
因為TestBean的msg默認是hello world,可以在配置文件中自定義TestBean的msg信息
這就是自動提示的效果
然后重啟項目,重新訪問//localhost:8080/test/user
到這里就已經(jīng)結(jié)束了!還沒看出來starter的好處嗎?
到此這篇關(guān)于SpringBoot手寫自定義starter源碼的文章就介紹到這了,更多相關(guān)自定義starter源碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot整合MongoDB實現(xiàn)文件上傳下載刪除
這篇文章主要介紹了SpringBoot整合MongoDB實現(xiàn)文件上傳下載刪除的方法,幫助大家更好的理解和學(xué)習(xí)使用SpringBoot框架,感興趣的朋友可以了解下2021-05-05java聯(lián)調(diào)生成測試數(shù)據(jù)工具類方式
這篇文章主要介紹了java聯(lián)調(diào)生成測試數(shù)據(jù)工具類方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03詳解在Spring MVC中使用注解的方式校驗RequestParams
本篇文章主要介紹了詳解在Spring MVC中使用注解的方式校驗RequestParams ,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-03-03SpringBoot整合Mybatis-Plus+Druid實現(xiàn)多數(shù)據(jù)源配置功能
本文主要講解springboot?+mybatisplus?+?druid?實現(xiàn)多數(shù)據(jù)源配置功能以及一些必要的準備及代碼說明,具有一定的參考價值,感興趣的小伙伴可以借鑒一下2023-06-06