SpringBoot手寫自定義starter源碼
1,前言
(1)SpringBoot的優(yōu)點(diǎn)
SpringBoot是新一代流行的Spring應(yīng)用開發(fā)框架,它具有更多的優(yōu)點(diǎn):
- 創(chuàng)建獨(dú)立的Spring應(yīng)用
- 內(nèi)嵌Tomcat、Jetty或Undertow(無需部署war包)
- 提供自用的starter來簡化構(gòu)建配置
- 提供指標(biāo)監(jiān)控、運(yù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等,各自都代表了一個(gè)相對完整的功能模塊。
SpringBoot-starter是一個(gè)集成接合器,完成兩件事:
- 引入模塊所需的相關(guān)jar包
- 自動(dòng)配置各自模塊所需的屬性
2,為什么要自定義starter?
在我們的日常開發(fā)工作中,經(jīng)常會(huì)有一些獨(dú)立于業(yè)務(wù)之外的配置模塊,我們經(jīng)常將其放到一個(gè)特定的包下,然后如果另一個(gè)工程需要復(fù)用這塊功能的時(shí)候,需要將代碼硬拷貝到另一個(gè)工程,重新集成一遍,麻煩至極。如果我們將這些可獨(dú)立于業(yè)務(wù)代碼之外的功配置模塊封裝成一個(gè)個(gè)starter,復(fù)用的時(shí)候只需要將其在pom中引用依賴即可,SpringBoot為我們完成自動(dòng)裝配,簡直不要太爽。
3,自定義starter的實(shí)現(xiàn)方式
首先新建一個(gè)maven工程,切記,只是一個(gè)maven工程而已,并非springboot工程!
(1)引入自動(dòng)配置依賴和自動(dòng)提示依賴
自動(dòng)提示就是在properties或yml配置文件中輸入時(shí)自動(dòng)提示!
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>2.0.3.RELEASE</version>
</dependency>
<!--輸入properties或yml會(huì)自動(dòng)提示-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>2.1.3.RELEASE</version>
</dependency>
(2)寫一個(gè)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)寫一個(gè)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)寫一個(gè)自動(dòng)配置類 MyAutoConfiguration
引用定義好的配置信息;在AutoConfiguration中實(shí)現(xiàn)所有starter應(yīng)該完成的操作,并且把這個(gè)類加入spring.factories配置文件中進(jìn)行聲明
@Configuration
@ConditionalOnClass({TestBean.class})//判斷當(dāng)前classpath下是否存在指定類,若是則將當(dāng)前的配置裝載入spring容器
@EnableConfigurationProperties(HelloServiceProperties.class)//激活自動(dòng)配置(指定文件中的配置)
public class MyAutoConfiguration {
@Autowired
HelloServiceProperties helloServiceProperties;//注入測試的配置信息類
@Bean
@ConditionalOnMissingBean(TestBean.class) //當(dāng)前上下文中沒有TestBean實(shí)例時(shí)創(chuàng)建實(shí)例
public TestBean getTestService(){
TestBean testBean=new TestBean();
testBean.setMsg(helloServiceProperties.getMsg());
return testBean;
}
}
完成上面的操作還不夠,最重要的一步是接下來的一步 在resources文件夾下新建文件夾META-INF/spring.factories,將上面的自定義配置類MyAutoConfiguration的全路徑名+類名配置到該文件中(遵循spring.factories的格式),這樣隨著項(xiàng)目的啟動(dòng)就可以實(shí)現(xiàn)自動(dòng)裝配! 文件內(nèi)容
# Auto Configure org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.ftx.tomcat.bean.MyAutoConfiguration
(5)把本maven項(xiàng)目打包,上傳到maven私服
使用maven構(gòu)建工具打包自定義starter項(xiàng)目
mvn clean install
然后上傳到maven私服上,再新建一個(gè)springboot項(xiàng)目,把私服和starter項(xiàng)目的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項(xiàng)目中寫一個(gè)TestController進(jìn)行測試 注入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();
}
}
然后啟動(dòng)項(xiàng)目,訪問//localhost:8080/test/user

因?yàn)門estBean的msg默認(rèn)是hello world,可以在配置文件中自定義TestBean的msg信息

這就是自動(dòng)提示的效果

然后重啟項(xiàng)目,重新訪問//localhost:8080/test/user

到這里就已經(jīng)結(jié)束了!還沒看出來starter的好處嗎?
到此這篇關(guān)于SpringBoot手寫自定義starter源碼的文章就介紹到這了,更多相關(guān)自定義starter源碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java 取交集方法retainAll的實(shí)現(xiàn)
這篇文章主要介紹了java 取交集方法retainAll的實(shí)現(xiàn)操作,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
mybatis中sql語句CDATA標(biāo)簽的用法說明
這篇文章主要介紹了mybatis中sql語句CDATA標(biāo)簽的用法說明,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06

