@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()")