說明:springboot 官方給我們提供了很多啓動器如:elasticsearch,aop,redis...等等
但是實際開發中,可能不同公司的業務不同需要定製化一個通用的專屬的啓動器來滿足公司內部使用,提高開發效率。
本文將介紹怎麼自定義實現一個啓動器的demo流程。
一:項目結構,模式。
空項目:mystarter(用來把 啓動器和自動配置模塊 項目放在一起,明瞭)
裏面有兩個module:
1:啓動器 sglhello-spring-boot-starter
作用:只用來做依賴導入(啓動器裏面依賴自動配置模塊,這樣外部項目直接引用啓動器就可以了);
命名規範:
springboot官方的啓動器: spring-boot-starter-XXX 如:spring-boot-starter-jdbc
我們自定義的啓動器:XXX-spring-boot-starter 如:sglhello-spring-boot-starter
2:自動配置模塊 sglhello-spring-boot-starter-autoconfigurer
作用:具體實現啓動器的業務邏輯
命名規範:
XXX-spring-boot-starter-autoconfigurer
XXX最好跟啓動器的XXX保持一致!
二:項目創建
idea裏面 File -- New -- Project...
然後選擇創建一個空項目:
project name寫:mytarter
1:先創建一個啓動器項目module:
選擇普通的Maven項目
GroupId:com.sgl.mystarter
ArtifactId:sglhello-spring-boot-starter(也就是按照自定義啓動器的命名規範創建 XXX-spring-boot-starter)
next之後需要調整下項目名稱和路徑:
調整爲:
然後點擊完成,這樣啓動器我們就創建好了;
2:接着創建一個自動配置模塊項目module:
選擇 SpringInitializr 然後next
GroupId:com.sgl.mystarter
ArtifactId:sglhello-spring-boot-starter-autoconfigurer
(按照自定配置模塊的命名規範創建 XXX-spring-boot-starter-autoconfigurer)
勾選Web -- Spring Web
然後next 然後點finish完成自動配置模塊的創建。
三:項目配置
1:配置啓動器依賴(啓動器配置文件裏面添加對自動配置模塊項目的依賴)
2:配置自動配置模塊項目依賴
注意:這裏我們把 dependencies 裏面只留一個最基礎的springboot對starter的支持就行了
插件的引用,web的依賴都去掉
然後我們去掉了web依賴需要清理下目錄結構
刪除掉test目錄,配置文件,和springboot的啓動類,這些都不需要
四:編寫自動配置模塊裏的業務邏輯
HelloProperties:
/**
*@ClassName HelloProperties
*@Description TODO 讀取配置文件裏面 sglhello.hello 的內容 並綁定到 HelloProperties對象上
*@Author Ni Klaus
*@Date 2019/10/10 0010 下午 19:29
*@Version 1.0
*/
@ConfigurationProperties(prefix = "sglhello.hello")
public class HelloProperties {
private String start;
private String end;
public String getStart() {
return start;
}
public void setStart(String start) {
this.start = start;
}
public String getEnd() {
return end;
}
public void setEnd(String end) {
this.end = end;
}
}
HelloService:
/**
*@ClassName HelloService
*@Description TODO 簡單業務邏輯實現
*@Author Ni Klaus
*@Date 2019/10/10 0010 下午 19:47
*@Version 1.0
*/
public class HelloService {
//讀取配置類裏面的配置信息
HelloProperties helloProperties;
public HelloProperties getHelloProperties() {
return helloProperties;
}
//賦值配置對象
public void setHelloProperties(HelloProperties helloProperties) {
this.helloProperties = helloProperties;
}
//簡單業務邏輯
public String sayHellSgl(String name){
return helloProperties.getStart()+"-" +name + helloProperties.getEnd();
}
}
HelloServiceAutoConfiguration:
/**
*@ClassName HelloServiceAutoConfiguration
*@Description TODO
*@Author Ni Klaus
*@Date 2019/10/10 0010 下午 19:48
*@Version 1.0
*/
@Configuration//申明這是一個配置類
@ConditionalOnWebApplication//引用啓動器的項目是web應用此自動配置模塊才生效
@EnableConfigurationProperties(HelloProperties.class)//加載配置對象到容器
public class HelloServiceAutoConfiguration {
//注入配置對象
@Autowired
HelloProperties helloProperties;
@Bean//方法返回結果對象加載到容器
public HelloService helloService(){
//新建業務邏輯處理對象,並返回加載到容器中,
// 這樣引用啓動器的項目就可以 @Autowired HelloService 對象直接使用了
HelloService helloService = new HelloService();
helloService.setHelloProperties(helloProperties);
return helloService;
}
}
創建XXXAutoConfiguration的掃秒配置
應爲springboot再啓動的過程中會去掃描項目和所有項目依賴引用的jar包 類路徑下的META-INF目錄下的 spring.factories
配置讀取所有的攔截器,過濾器,自動配置XXXAutoConfiguration 等等
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.sgl.mystarter.sglhello.config.HelloServiceAutoConfiguration
流程:
這樣其他springboot 項目 引用了啓動器,因爲啓動器 依賴 自動配置模塊,然後也會掃描 自動配置模塊 的 類路徑下的META-INF目錄下的 spring.factories HelloServiceAutoConfiguration配置類就會被拿到,然後裏面的 helloService() 方法返回的HelloService對象就會被創建並且被@Bean 註解註冊到ioc容器裏面,這樣 springboot 項目 裏面就可以 通過@Autowired 註解使用 HelloService 對象了。
五:打包並在其它項目上面引入啓動器
使用maven 的 install 分別順序 的把 自動配置模塊項目 和 啓動器 項目 安裝到你的本機maven倉庫裏面
(應爲啓動器是依賴自動配置模塊的,所以先install 自動配置模塊再install 啓動器)
(如果你的啓動器是給開發組用的,最好把 配置模塊項目 和 啓動器 項目 安裝到相應的maven私服倉庫就行,這樣別的項目引用直接引用啓動器就可以了)
六:測試使用
只需要在springboot的項目裏添加啓動器依賴
<!--引入自定義啓動器模塊-->
<dependency>
<groupId>com.sgl.mystarter</groupId>
<artifactId>sglhello-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
編寫一個測試登錄入口:
@RestController
public class Login {
//直接注入使用啓動器已經加載到容器裏面的HelloService 對象
@Autowired
HelloService helloService;
@RequestMapping("/login")
public String login(){
return helloService.sayHellSgl("科比");
}
}
然後我們啓動這個springboot項目
發現我們的啓動器已經有效果了,只是沒讀取到配置信息
想配置什麼我只需要在當前這個springboot項目的主配置文件裏面配置好就行
應爲我們自動配置模塊裏面設置的sglhello.hello對象相關屬性
所以我們可以添加如下配置文件
如:
sglhello.hello.start=NBA
sglhello.hello.end=最棒的籃球運動員
繼續啓動項目,訪問http://127.0.0.1:8080/login
好了,已經可以了!嘿嘿
本教程只是一個SpringBoot 自定義實現一個啓動器starter 的demo,如果你的業務邏輯比較複雜,只需要在你的自動配置模塊項目裏完成你要處理的業務邏輯就行!
本項目源碼地址:https://github.com/Hak-L/mystarter