当前位置: 首页 > news >正文

springboot 切面拦截自定义注解

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

  • 依赖
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-aop</artifactId>
</dependency>

1. 定义自定义注解

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. 定义切面类

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. 使用自定义注解

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

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

4. 启用

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 方法执行前后,切面类中的前置通知和后置通知方法会被调用,并输出相应的日志信息。
这就是使用切面拦截自定义注解的基本步骤和示例,你可以根据实际需求对注解和切面进行更复杂的扩展和定制。

相关文章:

  • 50%时效提升!中巴新航线如何重构ebay跨境电商物流成本?
  • win7/win10/macos如何切换DNS,提升网络稳定性
  • 若依改用EasyCaptcha验证码
  • UE5在场景3D物体上播放本地视频(带声音)
  • 数据挖掘案例-电力负荷预测
  • L2-052 吉利矩阵分
  • Sentinel源码—3.ProcessorSlot的执行过程一
  • 第五章 5.2ESP32物联网应用:HTTP与Web服务器详细教学
  • dfs二叉树中的深搜(回溯、剪枝)--力扣129、814、230、257
  • SpringMVC学习(请求与响应。常见参数类型接收与响应。@RequestParam、@RequestBody的使用)(详细示例)
  • 阿里云集群开启debug
  • LangChain缓存嵌入技术完全指南:CacheBackedEmbedding原理与实践(附代码示例)
  • 遵守 Vue3 的单向数据流原则:父组件传递对象 + 子组件修改对象属性,安全地实现父子组件之间复杂对象的双向绑定示例代码及讲解
  • 道路运输安全员企业负责人考试内容与范围
  • Zookeeper 可观测性最佳实践
  • 【Qt】QWidget 核⼼属性详解
  • 为啥mac日历打不开浏览器
  • 《分布式软总线赋能老旧设备高效通信》
  • 分布式光伏电站运维难?Acrel-1000DP助力安全稳定运行
  • Mybatis-Plus的使用
  • 科讯cms 3g 网站设置/b站视频未能成功转码
  • 成品大香伊煮蕉免费在线/晋城seo
  • 可以免费做演播的听书网站/百度极速版
  • 通州网站建设全包/潍坊做网站哪家好
  • 广东网站设计品牌设计/网站整站优化公司
  • 那个网站可以做logo/竞价推广账户托管服务