1. 前言
1.1. SpringBoot starter 机制
SpringBoot 中的 starter 是一种非常重要的机制,能够抛弃以前在 Spring 中的繁杂配置,将其统一集成进 starter,应用者只需要在 maven 中引入 starter 依赖,SpringBoot 就能自动扫描到要加载的信息并启动相应的默认配置。starter 让我们摆脱了各种依赖库的处理以及需要配置各种信息的困扰。SpringBoot 会自动通过 classpath 路径下的类发现需要的 Bean,并注册进 IOC 容器。SpringBoot 提供了针对日常企业应用研发各种场景的 spring-boot-starter 依赖模块。所有这些依赖模块都遵循着约定成俗的默认配置,并允许我们调整这些配置,即遵循“约定大于配置”的理念。
1.2. 自定义 starter 的作用
在我们的日常开发工作中,经常会有一些独立于业务之外的配置模块,比如对 web 请求的日志打印。我们经常将其放到一个特定的包下,然后如果另一个工程需要复用这块功能的时候,需要将代码硬拷贝到另一个工程,重新集成一遍,这样会非常麻烦。如果我们将这些可独立于业务代码之外的功配置模块封装成一个个 starter,复用的时候只需要将其在 maven pom 中引用依赖即可,让 SpringBoot 为我们完成自动装配,提高开发效率。
1.3. 自定义 starter 的命名规则
SpringBoot提供的 starter 以 spring-boot-starter-xxx 的方式命名的。官方建议自定义的 starter 使用 xxx-spring-boot-starter 命名规则。以区分 SpringBoot 生态提供的 starter。如:mybatis-spring-boot-starter
1.4. 如何自定义starter
新建两个模块,命名规范: xxx-spring-boot-starter
- xxx-spring-boot-autoconfigure:自动配置核心代码
- xxx-spring-boot-starter:管理依赖
- ps:如果不需要将自动配置代码和依赖项管理分离开来,则可以将它们组合到一个模块中。但 SpringBoot 官方建议将两个模块分开。
在 xxx-spring-boot-autoconfigure 项目中
- 引入 spring-boot-autoconfigure 的 maven 依赖
- 创建自定义的 XXXProperties 类: 这个类的属性根据需要是要出现在配置文件中的。
- 创建自定义的类,实现自定义的功能。
- 创建自定义的 XXXAutoConfiguration 类:这个类用于做自动配置时的一些逻辑,需将上方自定义类进行 Bean 对象创建,同时也要让 XXXProperties 类生效。
- 创建自定义的 spring.factories 文件:在 resources/META-INF 创建一个 spring.factories 文件和 spring-configuration-metadata.json,spring-configuration-metadata.json 文件是用于在填写配置文件时的智能提示,可要可不要,有的话提示起来更友好。spring.factories用于导入自动配置类,必须要有。
- 在 xxx-spring-boot-starter 项目中引入 xxx-spring-boot-autoconfigure 依赖,其他项目使用该 starter 时只需要依赖 xxx-spring-boot-starter 即可
2. 代码实现
2.1. 自动配置类
核心点 1 : StarterAutoConfiguration 自动配置类
package cn.knightzz.starter.config;
import cn.knightzz.starter.handler.OpenApiHeaderHandler;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
/**
* @author 王天赐
* @title config
* @description 自定义自动装配类
* @create 2024-05-15 10:20
*/
@Configuration
@EnableConfigurationProperties(OpenApiConfig.class)
@ComponentScan("cn.knightzz.starter.handler")
public class StarterAutoConfiguration {
}
@EnableConfigurationProperties(OpenApiConfig.class)
用于导入其他的配置类
@ComponentScan("cn.knightzz.starter.handler")
用于扫描 Bean
2.2. 配置文件
**核心点 1 : resources/META-INF/**spring.factories 配置文件
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
cn.knightzz.starter.config.StarterAutoConfiguration
也可以手动在这里暴露 Bean
org.springframework.boot.autoconfigure.EnableAutoConfiguration = \
com.jokerku.autoconfigure.LogAutoConfigure
org.springframework.boot.SpringApplicationRunListener= \
com.jokerku.testListener
org.springframework.context.ApplicationContextInitializer= \
com.jokerku.testInitializer
#自定义接口
com.jokerku.service.TestService = com.jokerku.service.impl.TestServiceImpl
#如其一个接口有多个实现,如下配置:
org.springframework.boot.logging.LoggingSystemFactory=\
org.springframework.boot.logging.logback.LogbackLoggingSystem.Factory,\
org.springframework.boot.logging.log4j2.Log4J2LoggingSystem.Factory,\
org.springframework.boot.logging.java.JavaLoggingSystem.Factory
2.3. Bean
📌Tips: 前面已经配置过@ComponentScan("cn.knightzz.starter.handler")
package cn.knightzz.starter.handler;
import org.springframework.stereotype.Component;
/**
* @author 王天赐
* @title AuthHeaderHandler
* @description
* @create 2024-05-15 10:49
*/
@Component
public class AuthHeaderHandler {
public void handle() {
System.out.println("AuthHeaderHandler.handle");
}
}
2.4. Enable 注解方式开启
除了通过 spring.factories 配置文件 以外, 还可以通过注解的方式去开启
package cn.knightzz.starter.auto;
import cn.knightzz.starter.config.StarterAutoConfiguration;
import org.springframework.context.annotation.Import;
import java.lang.annotation.*;
/**
* @author 王天赐
* @title EnableAutoConfigStarter
* @description
* @create 2024-05-15 10:55
*/
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Import(StarterAutoConfiguration.class)
public @interface EnableAutoConfigStarter {
}
📌这里直接导入 @Import(StarterAutoConfiguration.class)
自动配置类既可
package cn.knightzz.starter;
import cn.knightzz.starter.auto.EnableAutoConfigStarter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScans;
/**
* @author 王天赐
* @title StarterTestApplication
* @description
* @create 2024-05-15 13:49
*/
@SpringBootApplication(scanBasePackages = "cn.knightzz.starter.controller")
@EnableAutoConfigStarter
public class StarterTestApplication {
public static void main(String[] args) {
SpringApplication.run(StarterTestApplication.class, args);
}
}