Spring Boot手写starter

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

相关推荐

  1. Spring Bootstarter

    2024-07-10 16:18:07       23 阅读
  2. SpringBoot(五)之整合AOP

    2024-07-10 16:18:07       33 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-10 16:18:07       49 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-10 16:18:07       53 阅读
  3. 在Django里面运行非项目文件

    2024-07-10 16:18:07       42 阅读
  4. Python语言-面向对象

    2024-07-10 16:18:07       53 阅读

热门阅读

  1. 【国产开源可视化引擎Meta2d.js】视频

    2024-07-10 16:18:07       21 阅读
  2. Apache Doris的分区与分桶原理解析

    2024-07-10 16:18:07       14 阅读
  3. Stream流的简单用法

    2024-07-10 16:18:07       25 阅读
  4. liunx上修改Firefox版本号

    2024-07-10 16:18:07       16 阅读
  5. PS设计新手如何学习?沈阳PS设计线下培训

    2024-07-10 16:18:07       19 阅读
  6. 深度学习进阶

    2024-07-10 16:18:07       15 阅读
  7. 提示学习的本质是KNN

    2024-07-10 16:18:07       18 阅读
  8. Tomcat

    Tomcat

    2024-07-10 16:18:07      17 阅读
  9. pytorch 源码阅读(1)——torch.complie

    2024-07-10 16:18:07       14 阅读
  10. weapp.socket.io.js

    2024-07-10 16:18:07       14 阅读
  11. 内网和外网的区别及应用

    2024-07-10 16:18:07       22 阅读