SpringBoot自定义Starter

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

  1. xxx-spring-boot-autoconfigure:自动配置核心代码
  2. xxx-spring-boot-starter:管理依赖
  3. ps:如果不需要将自动配置代码和依赖项管理分离开来,则可以将它们组合到一个模块中。但 SpringBoot 官方建议将两个模块分开

在 xxx-spring-boot-autoconfigure 项目中

  1. 引入 spring-boot-autoconfigure 的 maven 依赖
  2. 创建自定义的 XXXProperties 类: 这个类的属性根据需要是要出现在配置文件中的。
  3. 创建自定义的类,实现自定义的功能。
  4. 创建自定义的 XXXAutoConfiguration 类:这个类用于做自动配置时的一些逻辑,需将上方自定义类进行 Bean 对象创建,同时也要让 XXXProperties 类生效。
  5. 创建自定义的 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);
    }
}

3. 参考

SpringBoot进阶 - 自定义starter

SpringBoot starter 原理及如何自定义 starter

  • 5
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

兀坐晴窗独饮茶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值