spring学习笔记十六 重用切点表达式

来源:互联网 发布:js定时器重复执行 编辑:程序博客网 时间:2024/06/04 17:52

在使用注解时,@Before("execution(public int aop.impl.AtithmeticCalculator.*(..))"),

里面的表达式,在其他的通知里面也会被重复使用,所以这个表达式可以被重用


所以可以在前置通知这个方法之前,写一个类,存放切入点表达式

        /*
* 定义一个方法,用于声明切入点表达式,该方法中不需要添加其他代码
* 只用@Pointcut来声明切入点表达式
* */
@Pointcut("execution(public int aop.impl.AtithmeticCalculator.*(..))")
public void declareJoinPointExpression(){}

@Before("declareJoinPointExpression()")
public void beforeMethod(JoinPoint joinPoint){
String methodName=joinPoint.getSignature().getName();
Object[] args=joinPoint.getArgs();
System.out.println("The Method "+methodName+"begins"+Arrays.asList(args));

}

同包同类:直接将表达式替换成:declareJoinPointExpression()

同包不同类:写成:类名.declareJoinPointExpression()

不同包不同类:写成 :包名.类名.declareJoinPointExpression()