开发公司业务时,如何去封装公共通用的代码,常见的选择是工具类之类,但是如果我们又需要在其他项目中使用呢?工具类复制一份过去?还有就是定义成单独的模块,也很好,但其实有个更好的选择是使用自定义
starter,自定义的starter在使用起来会非常方便,这篇文章主要讲讲如何自定义starter,大家可以基于此去封装公司中的一些公共代码。
介绍 starter
starter是SpringBoot中非常重要的一个机制,他是基于约定优于配置的思想所衍生出来的,摒弃了以前像Spring中需要使用一个依赖,需要添加非常多的配置,starter会在引入依赖时自动扫描需要加载的默认模块及配置,发现我们所定义的Bean并自动注册到IOC容器中。
一、创建 SpringBoot 项目并引入依赖
命名规范
SpringBoot官方的starter命名规范为:spring-boot-starter-{name}- 自定义
starter命名规范为:{name}-spring-boot-starter
首先通过IDEA创建一个SpringBoot项目。
这里我通过一个简单的日志功能来实现自定义starter功能,创建一个mylog-spring-boot-starter项目。对应的pom文件:
xml
<?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.7.15</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.jk</groupId>
<artifactId>mylog-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mylog-spring-boot-starter</name>
<description>mylog-spring-boot-starter</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.28</version>
</dependency>
</dependencies>
</project>
引入的依赖:
spring-boot-autoconfigure:这个依赖是实现自动配置的核心依赖,它会根据定义的META-INF/spring.factories里面定义的类全路径去找到这个类,然后根据@Bean注解所描述的方法注册Bean到IOC容器中。spring-boot-configuration-processor:这个依赖是为了实现在application.yml中定义配置时的提示功能。lombok:实现类中自动补全getter、setter、构造器等方法。
二、定义核心的 Service
这个service即在业务项目引入这个starter时可以自动装配的类。
service.MyLogService:
java
import lombok.Data;
@Data
public class MyLogService {
private String prefix;
private String suffix;
public void print(String log) {
System.out.println(prefix + " " + log + " " + suffix);
}
}
定义了一个print方法,实现日志的打印,日志的前缀和后缀我们会在后面的Properties中定义一个默认的,也可以通过在引入这个starter的业务模块中通过配置文件修改。
三、定义 Properties
定义Properties类来实现JavaConfig功能,把application.yml或application.properties中的配置读取映射到Java类上。
config.MyLogProperties:
java
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
@Data
@ConfigurationProperties(prefix = "mylog")
public class MyLogProperties {
private String prefix = "myPrefix";
private String suffix = "mySuffix";
}
@Data:通过这个lombok的注解实现类的setter、getter、构造器方法的自动补全。@ConfigurationProperties:通过这个注解实现类与配置文件的绑定和值的注入,通过prefix可以定义在配置文件中的统一前缀。比如配置文件中mylog.prefix的值就会绑定到类中prefix属性上。
四、定义 AutoConfiguration
定义AutoConfiguration类,引入starter时,会自动配置的类,它会自动注册里面的@Bean到spring中IOC容器中。
config.MyLogAutoConfiguration:
java
import com.jk.mylogspringbootstarter.service.MyLogService;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableConfigurationProperties(MyLogProperties.class)
public class MyLogAutoConfiguration {
@Bean
public MyLogService demoModule(MyLogProperties properties) {
MyLogService myLogService = new MyLogService();
myLogService.setPrefix(properties.getPrefix());
myLogService.setSuffix(properties.getSuffix());
return myLogService;
}
}
@Configuration:表明这是SpringBoot中的配置类。@EnableConfigurationProperties:使我们之前定义的Properties生效。@Bean:引入starter时自动注册的Bean。
五、定义 spring.factory
在resources中创建META-INF目录并创建spring.factories文件。写入以下内容:
ini
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.jk.mylogspringbootstarter.config.MyLogAutoConfiguration
上面这行内容固定,表示自动配置的类路径,下面需写我们之前定义的AutoConfiguration的全路径。
到此我们自定义starter源代码部分就完成了,整体的代码结构如下:

六、打包 starter,并在项目中测试
在IDEA中通过maven install在进行打包

或者通过在命令行中执行mvn clear install来进行打包,打包后会把这个依赖包安装到我们本地maven仓库。
在业务项目中:
1. pom中引入对应的starter
xml
<dependency>
<groupId>com.jk</groupId>
<artifactId>mylog-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
2. 自动装配在starter中定义的MyLogService

3. 日志打印结果

可以看到日志的前缀和后缀为之前我们在Properties中定义的默认值。
4. 修改日志前缀、后缀配置
通过在业务项目中application.yml修改前缀和后缀

测试打印结果如下:

到此,自定义starter的整体开发与使用就完成了,如果有什么不明白的小伙伴,可以评论区留言讨论哦。希望大家多多点赞、收藏。