@Pointcut——切点表达式重用
来源:互联网 发布:109普陀秒的数据 编辑:程序博客网 时间:2024/05/16 16:55
关于切面的五种通知方法,这篇博客中已经介绍了,但是若几种切面方法用的切面表达式都是相同的情况下,此时没必要在每个切面通知方法上面标识重复的切面表达式,可以选择重用切面表达式。示例如下:
LogProxy 为这篇博客中切面。下面通过重用切面表达式的方法重新定义了切面——使用 @Pointcut 来声明切入点表达式.。
package lzj.com.spring.aop;import java.util.Arrays;import java.util.List;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.AfterReturning;import org.aspectj.lang.annotation.AfterThrowing;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.Pointcut;import org.springframework.core.annotation.Order;import org.springframework.stereotype.Component;@Order(2)@Aspect@Componentpublic class LogProxy { /*定义一个方法, 用于声明切入点表达式. 一般地, 该方法中再不需要添入其他的代码. 使用 @Pointcut 来声明切入点表达式. 后面的其他通知直接使用方法名来引用当前的切入点表达式. */ @Pointcut("execution(public int lzj.com.spring.aop.ArithmeticCalculator.*(int, int))") public void performance(){} @Before("performance()") public void beforMethod(JoinPoint point){ String methodName = point.getSignature().getName(); System.out.println("LogProxy切面>目标方法为" + methodName); } @After("performance()") public void afterMethod(JoinPoint point){ String methodName = point.getSignature().getName(); List<Object> args = Arrays.asList(point.getArgs()); System.out.println("调用后连接点方法为:" + methodName + ",参数为:" + args); } @AfterReturning(value="performance()", returning="result") public void afterReturning(JoinPoint point, Object result){ String methodName = point.getSignature().getName(); List<Object> args = Arrays.asList(point.getArgs()); System.out.println("连接点方法为:" + methodName + ",参数为:" + args + ",目标方法执行结果为:" + result); } @AfterThrowing(value="performance()", throwing="ex") public void afterReturning(JoinPoint point, NullPointerException ex){ String methodName = point.getSignature().getName(); List<Object> args = Arrays.asList(point.getArgs()); System.out.println("连接点方法为:" + methodName + ",参数为:" + args + ",异常为:" + ex); }}
这篇博客中另一个切面和LogProxy切面在同一个包下面,如果想重用LogProxy中的切面表达式,可以用下面的方式:
package lzj.com.spring.aop;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.springframework.core.annotation.Order;import org.springframework.stereotype.Component;@Order(1)@Component@Aspectpublic class ThirdMethod {// @Before("execution(public int lzj.com.spring.aop.ArithmeticCalculator.*(int, int))") /*重用LogProxy切面中的切面表达式*/ @Before("LogProxy.performance()") public void getInfo(JoinPoint joint){ String methodName = joint.getSignature().getName(); System.out.println("ThirdMethod切面>目标方法" + methodName); }}
另外,如果LogProxy和ThirdMethod 两个切面不在同一个包下面,可以用包全名进行限制。例如ThirdMethod 在B包下面,而LogProxy在A包下面,在ThirdMethod 切面中若想复用LogProxy中的切面表达式,可以如下设置:
@Before("A.LogProxy.performance()")
阅读全文
0 0
- @Pointcut——切点表达式重用
- spring学习笔记(19)——重用切点表达式
- 14.Spring_重用切点表达式
- Pointcut 切点
- Spring中切点表达式的重用
- spring学习笔记十六 重用切点表达式
- 切点pointcut的详解
- Spring-AOP:切面优先级和重用切点表达式
- spring学习笔记(21)----重用切点表达式
- Spring_20-21,切面的优先级&重用切点表达式
- Spring AOP : AspectJ Pointcut 切点
- 19.Spring学习笔记_重用切点表达式(by尚硅谷_佟刚)
- spring的返回通知、异常通知、环绕通知、切面的优先级与重用切点表达式
- Spring Aop(三)——Pointcut表达式介绍
- 切点表达式无法识别
- spring aop 切点表达式
- Spring AOP之通知(advice),连点(connetDoint),切点(pointcut),切点(aspect)
- Spring Aop(九)——基于正则表达式的Pointcut
- Go的坑我慢慢填
- git reabse -i 合并commit
- Excel在统计分析中的应用—第十一章—相关分析-简单线性相关-散点图法
- nginx: [alert] kill(1022, 1) failed (3: No such process)
- Ubuntu中的Hadoop
- @Pointcut——切点表达式重用
- jdk8中的PermGen永久代去哪里
- 十周第一次课 2017.12.18 安装PHP5、安装PHP7
- Git 的 4 个阶段的撤销更改
- linux kobject kobject_type kset
- JDBC详解系列(一)之流程
- ++ 符号
- C语言中指针的一些概念及用法
- linux下安装mysql数据库