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

@Transactional注解的切点匹配

        前面切点表达式的匹配方法有一定的局限性,当注解在类或接口上的时候不能匹配。下面是另一种处理注解的切点匹配方式。

1.类的准备

    static class T1{@Transactionalpublic void foo(){}public void bar(){}}@Transactionalstatic class T2{public void foo(){}}@Transactionalinterface I3{void foo();}static class T3 implements I3{public void foo(){}}

2.主要方法

public static void main(String[] args) throws NoSuchMethodException {/*AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();pointcut.setExpression("execution(* bar())");System.out.println(pointcut.matches(T1.class.getMethod("foo"),T1.class));System.out.println(pointcut.matches(T1.class.getMethod("bar"),T1.class));AspectJExpressionPointcut pointcut1 = new AspectJExpressionPointcut();pointcut1.setExpression("@annotation(org.springframework.transaction.annotation.Transactional)");System.out.println(pointcut1.matches(T1.class.getMethod("foo"),T1.class));System.out.println(pointcut1.matches(T1.class.getMethod("bar"),T1.class));
*/StaticMethodMatcherPointcut pointcut2 = new StaticMethodMatcherPointcut() {@Overridepublic boolean matches(Method method, Class<?> targetClass) {//检查方法上是否加了Transactional注解MergedAnnotations annotations = MergedAnnotations.from(method);if (annotations.isPresent(Transactional.class)) {return true;}//查看类上是否加了Transactional注解annotations = MergedAnnotations.from(targetClass,MergedAnnotations.SearchStrategy.TYPE_HIERARCHY);if (annotations.isPresent(Transactional.class)) {return true;}return false;}};System.out.println(pointcut2.matches(T1.class.getMethod("foo"),T1.class));System.out.println(pointcut2.matches(T1.class.getMethod("bar"),T1.class));System.out.println(pointcut2.matches(T2.class.getMethod("foo"),T2.class));System.out.println(pointcut2.matches(T3.class.getMethod("foo"),T3.class));}

使用StaticMethodMatcherPointcut进行注解的匹配。

       1)重写matches方法

       2)检查方法上的注解

       3)检查类上是否加了注解

 annotations = MergedAnnotations.from(targetClass,MergedAnnotations.SearchStrategy.TYPE_HIERARCHY);

该方法会搜索类上以及继承树上的类或接口。

结果打印:

http://www.dtcms.com/a/589199.html

相关文章:

  • 建设网站 请示 报告淘宝网站制作公司哪家好
  • leetcode1770.执行乘法运算的最大分数
  • 本溪市城乡住房建设厅网站国外做网站侵权
  • 虚拟化入门笔记
  • 物联网设备运维中的自适应硬件老化监测与寿命预测技术
  • dede网站建设360商城官网
  • 【云运维】LNMP 架构部署与应用
  • 【最长连续序列】
  • FreeRTOS抢占调度与时间片调度
  • AI 编程翻车实录 - 智谱 GLM 4.6 给的自信
  • Anygrasp_sdk本地部署和使用
  • 系统设计 --- 多节点中按顺序处理消息
  • 深圳高端企业官方网站建设海创网站建设
  • 【C#】何时用ref?
  • STM32中ADC + DMA自动采集系统
  • 读书之《架构师的自我修炼》_个人笔记
  • 什么网站可以快速做3d效果图php网站维护
  • 迅速提高网站排名帮别人做网站收多少钱合适
  • 怎么做网站上的模拟动画做网站四百全包
  • 利用短整数类型和部分字符串优化DuckDB利用数组求解数独SQL
  • 营销型网站四大功能模版网站有源代码吗
  • 力扣81. 搜索旋转排序数组 II
  • WampServer安装教程(图文步骤)+ 下载+配置+解决图标红橙绿问题【附安装包】
  • 使用 dash 构建 mvvm 整洁架构应用
  • 跨时钟域处理
  • 2025制品管理工具选型,jfrog vs nexus vs hadess哪一款更好用?
  • 北京校园网站建设wordpress新建页面是
  • 【1.7】基于FPGA的costas环开发1——发射端信号产生模块
  • Postman 工具实现签名校验:完整操作指南与代码解析
  • AQS介绍