Spring Boot Starter介绍和实战

引言

Spring Boot Starter 是 Spring Boot 提供的一种机制,用于简化和集成应用程序的依赖管理。通过创建自定义的 Starter,可以将一组相关的依赖打包成一个简单的、可重用的模块,使应用程序的配置和依赖管理更加方便。在本文中,我们将深入探讨 Spring Boot Starter 的原理、创建过程,并通过实际示例演示其用法。

1. Spring Boot Starter 简介

Spring Boot Starter 是 Spring Boot 提供的一种约定,用于简化应用程序的依赖管理。它定义了一组通用的模块,每个模块关注一个特定的领域,例如数据库访问、消息队列、缓存等。这样,开发者可以根据需求选择相应的 Starter,并通过简单的配置即可引入所需的依赖。

Spring Boot Starter 的命名约定为 spring-boot-starter-*,例如:

  • spring-boot-starter-data-jpa: 用于数据持久化的 Starter。
  • spring-boot-starter-web: 用于构建 Web 应用程序的 Starter。
  • spring-boot-starter-actuator: 提供生产就绪功能的 Starter。

2. Spring Boot Starter 实战

2.1. 创建自定义 Starter

下面演示如何在 Spring Boot Starter 中使用注解记录操作审计的功能。

首先创建一个 Maven 项目作为 Spring Boot Starter。在项目中添加以下依赖:

XML 复制代码
<!-- pom.xml -->
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-autoconfigure</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
</dependencies>

2.2. 实现审计注解和切面

创建一个自动配置类 AuditAutoConfiguration,用于配置审计功能:

java 复制代码
// AuditAutoConfiguration.java
@Configuration
@EnableConfigurationProperties(AuditProperties.class)
public class AuditAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public AuditAspect auditAspect() {
        return new AuditAspect();
    }
}

创建一个属性配置类 AuditProperties,用于配置审计属性:

java 复制代码
// AuditProperties.java
@ConfigurationProperties(prefix = "audit")
public class AuditProperties {

    private boolean enabled = true;

    // Getter and Setter
}

创建一个审计切面类 AuditAspect,用于拦截带有审计注解的方法:

java 复制代码
// AuditAspect.java
@Aspect
@Component
@Slf4j
public class AuditAspect {
    
    @Autowired
    private AuditProperties auditProperties;

    @Around("@annotation(auditAnnotation)")
    public Object audit(ProceedingJoinPoint joinPoint, AuditAnnotation auditAnnotation) throws Throwable {
        if (auditProperties.isEnabled()) {
            String message = auditAnnotation.value();
            System.out.println("Audit Log: " + message);
        }
        // TODO 实现具体的审计记录逻辑 发送到kafka 
        // 或者将操作记录保存到数据库

        return joinPoint.proceed();
    }
}

2.3. 创建审计注解

创建一个审计注解 Audit,用于标记需要进行审计的方法:

java 复制代码
// Audit.java
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Audit {

    String value() default "";
}

2.4. 开启自启动扫描配置

在resources下创建META-INF目录,并创建spring.factories文件,将AuditAspect类完整路径写进去,否则该配置类无法被spring扫描到

java 复制代码
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  com.xxx.fly.log.aspect.AuditAspect

2.5. 创建 Starter 模块

创建 spring-boot-starter-audit 模块,将自动配置类、属性配置类、审计切面类和审计注解类打包成一个 JAR 文件。

2.6. 使用自定义 Starter 进行审计

引入自定义 Starter 依赖到 Spring Boot 项目中:

XML 复制代码
<!-- pom.xml -->
<dependencies>
    <dependency>
        <groupId>com.example</groupId>
        <artifactId>spring-boot-starter-audit</artifactId>
        <version>1.0.0</version>
    </dependency>
</dependencies>

配置文件中设置属性

在**application.propertiesapplication.yml** 中设置 **audit.enabled**属性开启审计功能

java 复制代码
# application.properties
audit.enabled=true

# application.yml
audit:
  enabled: true

在应用程序的方法上使用 **@Audit**注解进行审计:

java 复制代码
// MyService.java
@Service
public class MyService {

    @Audit("用户模块-新增用户")
    public void performAuditAction() {
        // 实际的业务逻辑
    }
}

3. 总结

通过本文的介绍和实战,我们深入了解了 Spring Boot Starter 的机制和用法。自定义 Starter 提供了一种有效的方式来组织和共享项目中的依赖关系,使应用程序的配置更加清晰、灵活。通过引入 Starter,开发者可以轻松地集成常用的功能模块,提高开发效率,使代码更加模块化和可维护。希望本文对大家理解和使用 Spring Boot Starter 有所帮助。

相关推荐
子兮曰16 小时前
jev-ultrafast 深度解析:7 秒订机票的浏览器 Agent 是如何炼成的
前端·后端·agent
小羊没烦恼!16 小时前
微服务化的基石——持续集成
java·大数据·word·powerpoint·.net
子兮曰16 小时前
Jev 爆发一周:7 秒 Agent 背后的 System One 生态与三场争议
前端·后端·ai编程
俊昭喜喜里16 小时前
java中的继承和多态的区别
java
小羊没烦恼!17 小时前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
爱勇宝17 小时前
ZCode 开源 24 小时:一份没有历史的账本,回答不了"有没有偷代码"
前端·后端·chatglm (智谱)
譕痕17 小时前
JSONObject与JSONArray封装数据格式区别
java·json
胡写代码17 小时前
别再前后端各写一套表单校验了
java·后端
小鱼能吃糖17 小时前
缺陷修复总览 · mall电商项目:5类缺陷,1个病根,4个业务域
java·电商
此时不提桶,更待何时17 小时前
01-06-A-JVM排查实战详解
java·jvm