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 有所帮助。

相关推荐
Larcher6 小时前
React Router 不只是页面跳转:从 SPA 路由到权限守卫的完整实践
javascript·后端
Larcher6 小时前
大模型为什么每次回答都不一样?一文搞懂 Temperature、Top K 与 Top P
javascript·后端
LucianaiB8 小时前
毕业老学长给我留的最后一句话是:“AI 写的,别挂我名。” 我直接 神(Seed Evolving) 来!助我!
后端
IvanCodes8 小时前
RAG 实战教程(一):RAG 工作原理与完整流程——分片、索引、召回、重排和生成
人工智能·后端·agent
·薯条大王8 小时前
经济实惠玩云服务器|一台云服务器多人共用,子账号配置教程
java·linux·运维·服务器·汇编·c++·python
CodeSheep9 小时前
稚晖君公司人事大变动,来了!
前端·后端·程序员
小满zs9 小时前
Go语言第八章(函数)
后端·go
stark张宇11 小时前
实战Go高级特性:Context超时控制、defer资源回收与Channel通信的关键避坑点
后端·go
ZJH__GO11 小时前
网络编程v4pro--实现聊天室文件传输功能
java·服务器·网络·计算机网络
程序员黑豆12 小时前
Windows 系统 Java 环境变量配置全攻略:解决“不是内部或外部命令”
java·前端·ai编程