1. 首页
  2. 软件开发
  3. JAVA
  4. Spring Boot手写starter

Spring Boot手写starter

  • 发布于 2024-08-15
  • 0 次阅读

本文由 简悦 SimpRead 转码, 原文地址 blog.csdn.net

目录

  1. 介绍

1.1 什么是 Starter 机制?

Starter 机制是 Spring Boot 提供的一种约定优于配置的实现方式,可以抛弃以前繁杂的配置,将其统一集成进 starter,应用者只需要在 maven 中引入 starter 依赖,SpringBoot 就能自动扫描到要加载的信息并启动相应的默认配置。

starter 让我们摆脱了各种依赖库的处理,需要配置各种信息的困扰。SpringBoot 会自动通过classpath路径下的类发现需要的 Bean,并注册进 IOC 容器。

1.2 Starter 机制的工作原理?

当 Spring Boot 应用启动时,它会扫描 classpath 下的 META-INF/spring.factories 文件,查找所有标记为 org.springframework.boot.autoconfigure.EnableAutoConfiguration 的配置类。这些配置类中定义了应用所需的 Bean 和自动配置逻辑。

Spring Boot 会根据条件注解(如 @ConditionalOnClass、@ConditionalOnMissingBean 等)来决定是否进行自动配置。

2.starter 自定义

springboot 官方推荐使用格式:

  • springboot 官方:spring-boot-starter-xxx
  • 第三方开发者自定义:xxxx-spring-boot-starter

自定义一个的启动器,包含一个自定义的记录方法执行时间的注解:logtimex-spring-boot-starter

2.1 新建工程

引入以下依赖:

<!--日志-->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
</dependency>
<!--aop相关-->
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
</dependency>
<!--工具类-->
<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
</dependency>
<!--自动配置相关-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-autoconfigure</artifactId>
</dependency>

2.2 自定义注解

package cn.smart.pagex.annotation;

import java.lang.annotation.*;
@Target({ElementType.METHOD})		//放在方法上
@Retention(RetentionPolicy.RUNTIME)		//运行时注解可见(用于通过反射读取注解)
@Documented

public @interface PageX {

    String value() default "";
}

2.3 创建增强类

package cn.smart.logtimex.aop;

import cn.hutool.core.date.StopWatch;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Component
@Aspect
@Slf4j
public class LogTimeXAop {
	//指定注解为切入点
    @Pointcut("@annotation(cn.smart.core.annotation.LogTimeX)")
    public void pointCut() {}

    @Around("pointCut()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        StopWatch stopWatch = new StopWatch();
        //开始计时
        stopWatch.start();

		//放行被增强的方法
        Object result = pjp.proceed();

		//结束计时
        stopWatch.stop();
        log.debug("方法执行时间:{}", stopWatch.getTotalTimeMillis());
        return result;
    }
}

2.4 创建自动配置类

package cn.smart.logtimex.autoconfiguration;

import cn.smart.logtimex.aop.LogTimeXAop;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@Configuration
@Import(LogTimeXAop.class)
//开关
//cn.smart.logtimex.enable = true
@ConditionalOnProperty(prefix = "cn.smart.logtimex",value = "enable", havingValue = "true", matchIfMissing = true)
public class LogTimeXAutoConfiguration { //领头羊
}

2.5 配置自动配置

在resourecs文件目录下创建 META-INF,并创建我们自己的spring.factories,并把我们的 LogTimeXAutoConfiguration 添加进去

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
cn.smart.logtimex.autoconfiguration.LogTimeXAutoConfiguration

2.6 测试

在其他模块引入依赖

<!--输出执行时间-->
        <dependency>
            <groupId>cn.smart</groupId>
            <artifactId>logtimex-spring-boot-starter</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

在application.properties配置文件中可以对我们的 starter 进行设置

//使starter有效,默认
cn.smart.logtimex.enable = true

//使starter无效,原理就是控制是否进行自动配置
cn.smart.logtimex.enable = false