springboot 切面拦截自定义注解

使用切面来拦截被该注解标记的方法

  • 依赖
xml 复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

1. 定义自定义注解

java 复制代码
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyCustomAnnotation {
    // 可以根据需要定义注解的属性
    String value() default "";
}

上述代码定义了一个名为 MyCustomAnnotation 的自定义注解,@Retention(RetentionPolicy.RUNTIME) 表示该注解在运行时仍然有效,@Target(ElementType.METHOD) 表示该注解只能用于方法上。

2. 定义切面类

java 复制代码
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class CustomAnnotationAspect {

    // 定义切入点表达式,拦截被 MyCustomAnnotation 注解标记的方法
    @Before("@annotation(myCustomAnnotation)")
    public void beforeMethod(JoinPoint joinPoint, MyCustomAnnotation myCustomAnnotation) {
        System.out.println("方法 " + joinPoint.getSignature().getName() + " 执行前,自定义注解属性值: " + myCustomAnnotation.value());
    }

    @After("@annotation(myCustomAnnotation)")
    public void afterMethod(JoinPoint joinPoint, MyCustomAnnotation myCustomAnnotation) {
        System.out.println("方法 " + joinPoint.getSignature().getName() + " 执行后,自定义注解属性值: " + myCustomAnnotation.value());
    }
}

在这个切面类中:

@Aspect 注解表明该类是一个切面类。

@Before("@annotation(myCustomAnnotation)") 定义了一个前置通知,当被 MyCustomAnnotation 注解标记的方法执行前,这个方法会被调用。

@After("@annotation(myCustomAnnotation)") 定义了一个后置通知,当被 MyCustomAnnotation 注解标记的方法执行后,这个方法会被调用。

JoinPoint 参数可以获取到被拦截方法的相关信息,如方法签名等。

3. 使用自定义注解

java 复制代码
import org.springframework.stereotype.Service;

@Service
public class MyService {

    @MyCustomAnnotation(value = "示例注解属性值")
    public void myMethod() {
        System.out.println("执行 myMethod 方法");
    }
}

在 MyService 类的 myMethod 方法上使用了 MyCustomAnnotation 注解。

4. 启用

java 复制代码
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@SpringBootApplication
@EnableAspectJAutoProxy
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

运行测试代码后,会看到在 myMethod 方法执行前后,切面类中的前置通知和后置通知方法会被调用,并输出相应的日志信息。

这就是使用切面拦截自定义注解的基本步骤和示例,你可以根据实际需求对注解和切面进行更复杂的扩展和定制。

相关推荐
那小子、真烦9 分钟前
Hermes Agent Chat 方法分析
java·开发语言
01_ice25 分钟前
Java抽象类和接口
java·开发语言
小马爱打代码30 分钟前
Spring源码 第七篇:Spring Boot 自动配置原理深度拆解
java·spring boot·spring
日取其半万世不竭39 分钟前
给 Docker 容器设置 CPU 和内存限制,避免单个服务拖垮整机
java·docker·容器
铁皮哥43 分钟前
【agent 开发】Claude Code 的 Skill 是怎么被加载的?从 name/description 到 SKILL.md 再到资源文件
java·服务器·数据库·python·gitee·github·软件工程
白宇横流学长1 小时前
基于SpringBoot实现的校园失物招领平台设计与实现【源码+文档】
java·spring boot·后端
罗超驿1 小时前
6.Java多线程详解:Thread类、线程属性与start()方法深度解析
java·开发语言·面试·java-ee
苦逼的猿宝1 小时前
IT技术交流和分享平台的设计与实现(源码+论文)
java·毕业设计·springboot·计算机毕业设计
摇滚侠1 小时前
IDEA 需要修改的配置 开发工具
java·ide·intellij-idea
2601_957786771 小时前
企业矩阵运营的“三段论“:管号、产内容、获线索——全链路系统的价值拆解
java·前端·矩阵·多平台管理