前言
SpringBoot能够实现快速开发,其中一点是底层为我们自动化配置好了一个个Starter,我们只需根据自身需求,修改相应配置项即可。那如何来定制化一个Starter,实现功能的复用呢?
定制化mytest-spring-boot-starter
编码之前,我们可以回顾一下SpringBoot的自动装配流程,这里我简单以导入mybatis的starter为例。
<!--导入的mybatis-spring-boot-starter-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<!--底层引入相关包-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
</dependency>
我们看到引入mybatis-spring-boot-starter
,底层也会响应引入mybatis-spring-boot-autoconfigure
的自动配置包。点进mybatis-spring-boot-autoconfigure
看到更核心的依赖:
<!-- Compile dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<!-- Optional dependencies -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<optional>true</optional>
</dependency>
看到这,我们大致清楚整个依赖的关系:mybatis-spring-boot-starter->mybatis-spring-boot-autoconfigure->spring-boot-autoconfigure;所以我们如果定制化场景器的话,提供xxx-spring-boot-starter作为依赖供其他应用引入,而重点实现逻辑关注xxx-spring-boot-autoconfigure,并且底层以spring-boot-autoconfigure作为核心组件。
自定义mytest-spring-boot-starter
和mytest-spring-boot-starter-autoconfigure
在同一个空项目下初始化两个子工程,命名分别是mytest-spring-boot-starter
和mytest-spring-boot-starter-autoconfigure
mytest-spring-boot-starte工程作为外界引入包,其中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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.gz.xf</groupId>
<artifactId>mytest-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
<!--引入自定义配置包-->
<dependencies>
<dependency>
<groupId>com.gz.xf</groupId>
<artifactId>mytest-spring-boot-starter-autoconfigure</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
mytest-spring-boot-starter-autoconfigure作为自动配置包,需要提供SpringBoot自动装配功能,所以要也是要单独作为SpringBoot引用。不过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>
<groupId>com.gz.xf</groupId>
<artifactId>mytest-spring-boot-starter-autoconfigure</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mytest-spring-boot-starter-autoconfigure</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-boot.version>2.3.7.RELEASE</spring-boot.version>
</properties>
<dependencies>
<!--SpringBoot核心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>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
- 需要复用的组件HelloService
/**
* @Description
* @Author Fangchenjiang
* @Date 2021/10/11 9:37
*/
public class HelloService {
@Autowired
HelloProperties helloProperties;
public String sayHello(String name) {
return helloProperties.getPrefix() + ":" + name + ":" + helloProperties.getSuffix();
}
}
- 配置项绑定类
/**
* @Description
* @Author Fangchenjiang
* @Date 2021/10/11 9:37
*/
@ConfigurationProperties(prefix = "hello")
class HelloProperties {
String prefix;
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 Fangchenjiang
* @Date 2021/10/11 9:43
*/
@Configuration
@EnableConfigurationProperties(HelloProperties.class) //开启属性配置绑定
public class HelloServiceAutoConfiguration {
@ConditionalOnMissingBean(HelloService.class) //若容器中没指定Bean才注册
@Bean
public HelloService helloService() {
return new HelloService();
}
}
- 包扫描文件
删掉原来SpringBoot类路径下的所有东西,创建META/spring.factories并指定开启自动配置的类路径
- 打包并安装
安装到本地仓库Maven提示:
[INFO] --- maven-install-plugin:2.4:install (default-install) @ mytest-spring-boot-starter ---
[INFO] Installing E:\code\mytest-spring-boot-starter\mytest-spring-boot-starter\target\mytest-spring-boot-starter-1.0-SNAPSHOT.jar to E:\Local\maveRepository\com\gz\xf\mytest-spring-boot-starter\1.0-SNAPSHOT\mytest-spring-boot-starter-1.0-SNAPSHOT.jar
[INFO] Installing E:\code\mytest-spring-boot-starter\mytest-spring-boot-starter\pom.xml to E:\Local\maveRepository\com\gz\xf\mytest-spring-boot-starter\1.0-SNAPSHOT\mytest-spring-boot-starter-1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
引入并使用
新建某个工程,引入本地定制化的Starter依赖
<dependency>
<groupId>com.gz.xf</groupId>
<artifactId>mytest-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
/**
* @Description
* @Author Fangchenjiang
* @Date 2021/8/26 10:03
*/
@RestController
public class TestController {
/**
* 注入定制化Starter的组件
*/
@Autowired
HelloService helloService;
@GetMapping("/test")
public String test() {
return helloService.sayHello("小方");
}
}
server:
port: 8888
spring:
application:
name: demo
#属性绑定
hello:
suffix: 123
prefix: 456
浏览器访问: