一、自定義starter
-
啓動器只用來做
依賴導入
-
專門來寫一個自動配置模塊;
-
啓動器
依賴
自動配置模塊,項目中只需要引入相應的starter
就會引入啓動器的所有傳遞
依賴
也就是說xxx-starter(啓動器)依賴於xxxx-starter-autoconfigurer(自動配置), 別人要使用,就依賴xxx-starter啓動器,就自動引入了自動配置。
1、啓動器
啓動器模塊是一個空JAR
文件,僅提供輔助性依賴管理
,這些依賴可能用於自動裝配或者其他類庫。
2、命名規約
-
官方命名
spring-boot-starter-模塊名
eg:
spring-boot-starter-web、spring-boot-starter-jdbc、spring-boot-starter-thymeleaf -
自定義命名
模塊名-spring-boot-starter
eg:
mybatis-spring-boot-start
3、如何編寫自動配置
@Configuration //指定這個類是一個配置類
@ConditionalOnXXX //在指定條件成立的情況下自動配置類生效
@AutoConfigureAfter //指定自動配置類的順序
@Bean //給容器中添加組件
@ConfigurationPropertie結合相關xxxProperties類來綁定相關的配置
@EnableConfigurationProperties //讓xxxProperties生效加入到容器中
public class XxxxAutoConfiguration {
自動配置類要能加載,需要將啓動就加載的自動配置類配置在
META-INF/spring.factories
中
二、案例 (自定義啓動器)
- 首先創建一個項目, 這個項目中創建兩個模塊; 一個模塊是一個Maven模塊(
啓動器
), 一個模塊是一個SpringBoot模塊(自動配置模塊
)
1、自動配置模塊
1.
創建一個自動配置模塊
,和創建普通springboot項目一樣,不需要引入其他starter- 刪除掉多餘的文件和依賴
pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.zy.starter</groupId>
<artifactId>zy-spring-boot-starter-autoconfigurer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>zy-spring-boot-starter-autoconfigurer</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- 引入spring-boot-starter; 所有starter的基本配置 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!--可以生成配置類提示文件-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</project>
2.
創建配置類和自動配置類
配置類
/**
* Description:
*
* @author zygui
* @date 2020/4/19 11:00
*/
@ConfigurationProperties(prefix = "gzy")
public class GzyProperties {
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;
}
}
自動配置類
/**
* Description: 自動配置類
*
* @author zygui
* @date 2020/4/19 11:05
*/
@Configuration
@ConditionalOnWebApplication // web應用才生效
@EnableConfigurationProperties(GzyProperties.class) //讓配置類生效,(注入到容器中)
public class GzyAutoConfiguration {
@Autowired
private GzyProperties gzyProperties;
@Bean
public HelloService helloService() {
HelloService service = new HelloService();
service.setGzyProperties(gzyProperties);
return service;
}
}
HelloService類
public class HelloService {
GzyProperties gzyProperties;
public GzyProperties getGzyProperties() {
return gzyProperties;
}
public void setGzyProperties(GzyProperties gzyProperties) {
this.gzyProperties = gzyProperties;
}
public String sayHelloGzy(String name) {
return gzyProperties.getPrefix() + "-" + name + gzyProperties.getSuffix();
}
}
這裏也可以省略HelloService, 將其作爲自動配置類
的一個內部類, SpringBoot源碼大多也是這樣寫的; 具體看下面
配置類不變
自動配置類
/**
* Description: 自動配置類
*
* @author zygui
* @date 2020/4/19 11:05
*/
@Configuration
@ConditionalOnWebApplication // web應用才生效
@EnableConfigurationProperties(GzyProperties.class) //讓配置類生效,(注入到容器中)
public class GzyAutoConfiguration {
private final GzyProperties gzyProperties;
/**
* 通過構造器注入
*
* @param gzyProperties
*/
public GzyAutoConfiguration(GzyProperties gzyProperties) {
this.gzyProperties = gzyProperties;
}
@Bean
public HelloService helloService() {
return new HelloService();
}
public class HelloService {
public String sayHelloGzy(String name) {
return gzyProperties.getPrefix() + "-" + name + "-" + gzyProperties.getSuffix();
}
}
}
3.
在resources文件夾下創建META-INF/spring.factories
spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
cn.clboy.spring.boot.autoconfigure.ClboyAutoConfiguration
4.
安裝到本地倉庫
2、創建自定義啓動器
5.
創建starter,選擇maven工程即可,只是用於管理依賴,添加對AutoConfiguration模塊的依賴
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zy</groupId>
<artifactId>zy-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- 啓動器 -->
<dependencies>
<!-- 在自定義的啓動器中, 引入自定義的自動配置模塊 -->
<dependency>
<groupId>com.zy.starter</groupId>
<artifactId>zy-spring-boot-starter-autoconfigurer</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
6.
安裝到本地倉庫
3、創建項目測試,選擇添加web場景,因爲設置是web場景才生效
7.
引入自定義的啓動器
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.zy</groupId>
<artifactId>springboot-09-starter-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-09-starter-test</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 引入自定義的starter -->
<dependency>
<groupId>com.zy</groupId>
<artifactId>zy-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
8.
創建Controller
@RestController
public class HelloController {
@Autowired
HelloService helloService;
@GetMapping("/hello")
public String hello(){
return helloService.sayHelloGzy("GZY");
}
}
9.
在application.properties配置文件中可以配置
gzy.prefix=HELLO
gzy.suffix=WORLD
也可以搜索GzyProperties
可以看到自定義XxxProperties
10.
測試
調用過程 :