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。在项目中添加以下依赖:

<!-- 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,用于配置审计功能:

// AuditAutoConfiguration.java
@Configuration
@EnableConfigurationProperties(AuditProperties.class)
public class AuditAutoConfiguration {

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

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

// AuditProperties.java
@ConfigurationProperties(prefix = "audit")
public class AuditProperties {

    private boolean enabled = true;

    // Getter and Setter
}

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

// 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,用于标记需要进行审计的方法:

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

    String value() default "";
}

2.4. 开启自启动扫描配置

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

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 项目中:

<!-- 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 属性开启审计功能

# application.properties
audit.enabled=true

# application.yml
audit:
  enabled: true

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

// MyService.java
@Service
public class MyService {

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

3. 总结

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Memory_2020

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

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

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

打赏作者

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

抵扣说明:

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

余额充值