首先,關於自定義啓動器命名問題:
官方命名示例:spring-boot-starter-web
第三方命名示例:mybatis-spring-boot-starter
所以我們自定義starter命名規則,可以借鑑mybatis。
即:自定義啓動器名-spring-boot-starter
其次,思路問題:
1).我們可以寫一個啓動器只用來做依賴導入
【例如:dmsdbj-spring-boot-starter】
2).然後專門寫一個自動配置模塊。
【例如:dmsdbj-spring-boot-strater-autoconfigurer】
3).啓動器依賴自動配置,調用者只需要引入啓動器(starter)
開始編寫代碼
1).新建兩個maven工程,然後如下圖建立相關文件
【有些無用的文件我刪除了,比如test文件夾】
2).在啓動器工程(dmsdbj-spring-boot-starter)的pom文件中,引入dmsdbj-spring-boot-strater-autoconfigurer工程的依賴。
<!--啓動器-->
<dependencies>
<!--引入自動配置模塊-->
<dependency>
<groupId>com.dmsdbj</groupId>
<artifactId>dmsdbj-spring-boot-strater-autoconfigurer</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
3).在 dmsdbj-spring-boot-strater-autoconfigurer 工程中,引入相關依賴(根據自定義starter功能,導入相關依賴)。
4).編寫dmsdbj-spring-boot-strater-autoconfigurer工程的業務邏輯功能。
我這裏的功能:
自定義一個HelloService類,該類中有sayHello( String name )方法,根據傳來的name字符串,然後給他拼接一個頭字符串和尾字符串,最後返回回去。其中,頭字符串和尾字符串均爲可配置的,即:由調用方來設定值(從調用方的配置文件中讀取。)
4.1,編寫HelloProperties類
【該類作用:】
從發起調用者那裏的配置文件,讀取到頭字符串和尾字符串的值,映射給該類中的對應字段。
package com.dmsdbj;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "dmsdbj.hello")
public class HelloProperties {
private String prefix;
private String suffix;
public String getPrefix() {
return prefix;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
public String getSuffix() {
return suffix;
}
public void setSuffix(String suffix) {
this.suffix = suffix;
}
}
4.2.編寫HelloService類
【該類作用:】
根據HelloProperties類中讀取的頭字符串和尾字符串,和調用者類傳來的name字符串,一起拼接成新的字符串,然後返回給調用者。
注意,到這一步,該HelloService類沒有被注入到容器中呢還!!!
package com.dmsdbj;
public class HelloService {
HelloProperties helloProperties;
public HelloProperties getHelloProperties() {
return helloProperties;
}
public void setHelloProperties(HelloProperties helloProperties) {
this.helloProperties = helloProperties;
}
public String sayHello(String name) {
return helloProperties.getPrefix() + "_" + name + "_" + helloProperties.getSuffix();
}
}
4.3.編寫HelloServiceAutoConfigruation類(自動配置類)。
【該類作用:】
激活 HelloProperties 屬性文件,讓該類生效。
爲 HelloService 類注入 HelloProperties。
然後把 HelloService 注入到容器。這樣調用者才能發現 HelloService 。
package com.dmsdbj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConditionalOnWebApplication // 指明這是web應用,纔會生效
@EnableConfigurationProperties(HelloProperties.class) // 讓HelloProperties 屬性文件生效
public class HelloServiceAutoConfigruation {
@Autowired
private HelloProperties helloProperties;
@Bean
public HelloService helloService() {
HelloService helloService = new HelloService();
helloService.setHelloProperties(helloProperties);
return helloService;
}
}
4.4.業務寫完了,問題是,HelloServiceAutoConfigruation 類又如何被發現呢??HelloServiceAutoConfigruation如果不被發現,不被執行,後面邏輯都是無用功。所以,在resources目錄下,新建 META-INF文件夾,然後新建spring.factories 文件。【注意,這裏路徑不能寫錯】
系統啓動後就想要加載的那些自動配置類,必須配置在META-INF/spring.factories中
編寫 spring.factories 文件。
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.dmsdbj.HelloServiceAutoConfigruation
大功告成,就差測試了。。。。
萬事俱備,只欠東風???
不,還沒測試呢啊。。。
新建web工程,可以使用嚮導新建(快速)。如下目錄結構:
1)。導入相關依賴,導入剛寫的的starter啓動器即可。
<!--引入自定義starter-->
<dependency>
<groupId>com.dmsdbj</groupId>
<artifactId>dmsdbj-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
有沒有發現導入不進來??報紅???-----因爲上面的兩個工程寫完了沒有install啊,所以本地倉庫當然找不到了啊(私服,中央倉庫更不可能有啊)
所以,去吧剛纔那兩工程install一下
【順序的話,先dmsdbj-spring-boot-strater-autoconfigurer 後 dmsdbj-spring-boot-strater吧】
2)。編寫頭字符串和尾字符串的配置文件
在測試工程15springboot_71_starter_test中的resources下的application.properties文件中:
dmsdbj.hello.prefix=touString
dmsdbj.hello.suffix=weiString
3)。編寫測試controller
package com.dmsdbj.controller;
import com.dmsdbj.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloControll {
@Autowired
HelloService helloService;
@GetMapping("/hello")
public String hello() {
return helloService.sayHello("haha");
}
}
4)。開始測試,運行工程(測試工程的主程序類的main方法),然後打開瀏覽器輸入“localhost:8080/hello”,回車,奇蹟出現了。。。
到此,自定義starter結束,關於上面有什麼疑問或者不妥,歡迎指出。