一、自定義starter
說到自定義starter(場景啓動器),要先明確以下幾點:
- 一是這個場景需要使用什麼依賴?
- 二是如何編寫springboot的自動配置?
@Configuration //指定這個類是一個配置類
@ConditionalOnXXX //在指定條件成立的情況下自動配置類生效 @AutoConfigureAfter //指定自動配置類的順序
@Bean //給容器中添加組件
@ConfigurationPropertie //結合相關xxxProperties類來綁定相關的配置
@EnableConfigurationProperties //讓xxxProperties生效加入到容器中
自動配置類要能加載 將需要啓動就加載的自動配置類,配置在META‐INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
- 三是自定義starter的模式
啓動器只用來做依賴導入,需要專門提供一個自動配置模塊:啓動器依賴於自動配置模塊,其他項目只需要引入啓動器即可。
命名規約(推薦使用以下命名規約):
【1】官方命名空間
– 前綴:“spring-boot-starter-”
– 模式:spring-boot-starter-模塊名
– 舉例:spring-boot-starter-web、spring-boot-starter-actuator、spring-boot-starter-jdbc
【2】自定義命名空間
– 後綴:“-spring-boot-starter”
– 模式:模塊-spring-boot-starter
– 舉例:mybatis-spring-boot-starter
二、自定義starter示例
【1】創建一個空的project
【2】在上一步創建的空項目中創建兩個Module
【3】配置啓動器和啓動器配置模塊
【3.1】配置啓動器配置模塊
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();
}
}
該自定義starter可以設置的屬性類:
//相當於寫了一個屬性,用來綁定"zm.hello"的配置
@ConfigurationProperties(prefix = "zm.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;
}
}
使屬性類生效的自動配置類:
@Configuration //指明這是一個配置類
@ConditionalOnWebApplication //在web應用中才生效
@EnableConfigurationProperties(HelloProperties.class) //導入屬性配置文件
public class HelloServiceAutoConfiguration {
@Autowired
HelloProperties helloProperties;
@Bean //將HelloService加入到容器中
public HelloService helloService(){
HelloService service = new HelloService();
service.setHelloProperties(helloProperties);
return service;
}
}
在resources
的目錄下創建META-INF/spring.factories
註冊自動配置類:
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.zm.starter.springboot.HelloServiceAutoConfiguration
【3.2】配置啓動器模塊:在pom.xml文件中導入自動配置模塊的依賴
<!--啓動器-->
<dependencies>
<!--引入自動配置模塊-->
<dependency>
<groupId>com.zm.starter</groupId>
<artifactId>springboot-starter-autoconfigure</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
【4】創建測試項目
controller:
@RestController
public class HelloController {
@Autowired
HelloService helloService;
@GetMapping("/hello")
public String sayHello(){
String sayHello = helloService.sayHello("Make");
return sayHello;
}
}
pom.xml文件的配置:
<!--引入自定義的啓動器-->
<dependency>
<groupId>com.zm.starter</groupId>
<artifactId>springboot-zm-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>