@Pointcut——切点表达式重用

关于切面的五种通知方法,这篇博客中已经介绍了,但是若几种切面方法用的切面表达式都是相同的情况下,此时没必要在每个切面通知方法上面标识重复的切面表达式,可以选择重用切面表达式。示例如下:

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
@Component
public 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
@Aspect
public 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()")
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值