需求
- 减少公用业务处理代码的重复性,提高代码可重用性
- 提高项目编码效率
应用场景
- 动态数据源配置
- 数据模型组件
- Redis操作组件
- 资源认证组件
- 文件操作组件
- 报表生成组件
- 网络操作工具组件
- MQTT连接组件
- …
创建自动义starter步骤
当前示例义创建一个普通项目为例
第一步创建一个maven项目并配置pom.xml
- 命名规则
- maven配置文件如下所示
<?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.dm</groupId>
<artifactId>dm-spring-boot-start</artifactId>
<name>dm-spring-boot-starter</name>
<version>0.0.1-RELEASE</version>
<description>Dm project for Spring Boot Starter</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--引入spring-boot-config;所有starter的基本配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.73</version>
</dependency>
</dependencies>
</project>
- 创建配置文件加载类
package com.dm.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "dm.mqtt") // 自动获取配置文件中前缀为http的属性,把值传入对象参数
@Data
public class DmProperties {
// 默认值
public static final Boolean DM_ENABLE = false;
private Boolean enable = false;
}
- 自动加载类
package com.dm.config;
import com.dm.service.DmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author hai
* @date 2022-06-22
*/
@Configuration // 自动加载
@EnableConfigurationProperties(DmProperties.class)
public class DmServiceAutoConfiguration {
@Autowired
private DmProperties dmProperties;
// 自动注入配置类
// public DmServiceAutoConfiguration(DmProperties dmProperties) {
// this.dmProperties = dmProperties;
// }
@Bean(name = "dmDemo")
public DmService helloService(){
DmService service = new DmService();
service.setDmProperties(dmProperties);
return service;
}
}
- 业务处理类
package com.dm.service;
import com.dm.config.DmProperties;
public class DmService {
DmProperties dmProperties;
public DmProperties getDmProperties() {
return dmProperties;
}
public void setDmProperties(DmProperties dmProperties) {
this.dmProperties = dmProperties;
}
}
【重点内容】在resource目录下创建文件/META-INF/spring.factories,内容如下所示
#-------starter自动装配---------
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.dm.config.DmServiceAutoConfiguration
Spring Factories原理
Spring-Core 包里定义了SpringFactoriesLoader类,这个类实现了检索WETA-INFO/spring.factories文件,并获取指定接口配置,此类中定义了两个对外的方法
- laodFactories 根据接口获取实现类示例,返回值为对象列表
- loadFactoriesNames 根据这个接口可以获取实现类名称、返回类名列表
删繁就简、简约生活