spring aop @Pointcut annotation extraction using reusable pointcut expression

For the same entry point, if there are multiple notification methods will be cut, usually we need to mark more annotations, each annotation entry points need to use expressions, tell spring, when the current notification method executed. When an entry point needs to be changed, it needs to change more. To avoid this inconvenience, promotes reuse pointcut expressions, derived @Pointcut thus, it may be used to mark an entry point.

Example:
as section classes previously in StudentServiceLogger, for StudentServiceImplall methods of the class are cut into four notice, if one day, I would like to change only when one cut method, you need to modify the entry point for the expression of these four notes .

@Aspect
@Component
public class StudentServiceLogger {

    @Before("execution(* com.wuwl.service.impl.StudentServiceImpl.*(..) )")
    public void doBefore(JoinPoint joinPoint){
        Object[] args = joinPoint.getArgs();
        String methodName = joinPoint.getSignature().getName();
        System.out.println(methodName+"方法执行前...");
        System.out.println("参数为:"+ Arrays.asList(args));
    }

    @After("execution(* com.wuwl.service.impl.StudentServiceImpl.*(..) )")
    public void doAfter(JoinPoint joinPoint){
        String methodName = joinPoint.getSignature().getName();
        System.out.println(methodName+"方法执行后...");
    }

    @AfterReturning(value = "execution(* com.wuwl.service.impl.StudentServiceImpl.*(..) )" , returning = "returning")
    public void doReturn(JoinPoint joinPoint,Object returning){
        String methodName = joinPoint.getSignature().getName();
        System.out.println(methodName+"方法返回,返回值为:"+returning);

    }

    @AfterThrowing(value = "execution(* com.wuwl.service.impl.StudentServiceImpl.*(..) )",throwing = "ex")
    public void doThrow(JoinPoint joinPoint,Exception ex){
        String methodName = joinPoint.getSignature().getName();
        System.out.println(methodName+"方法异常,异常信息为:"+ex.getMessage());
    }

}

@Pointcut example uses the
above example, fully equivalent to the following section of this class.

@Aspect
@Component
public class StudentServiceLogger {

    @Pointcut("execution(* com.wuwl.service.impl.StudentServiceImpl.*(..) )")
    public void basePointCut(){}
    
    @Before("basePointCut()")
    public void doBefore(JoinPoint joinPoint){
        Object[] args = joinPoint.getArgs();
        String methodName = joinPoint.getSignature().getName();
        System.out.println(methodName+"方法执行前...");
        System.out.println("参数为:"+ Arrays.asList(args));
    }

    @After("basePointCut()")
    public void doAfter(JoinPoint joinPoint){
        String methodName = joinPoint.getSignature().getName();
        System.out.println(methodName+"方法执行后...");
    }

    @AfterReturning(value = "basePointCut()" , returning = "returning")
    public void doReturn(JoinPoint joinPoint,Object returning){
        String methodName = joinPoint.getSignature().getName();
        System.out.println(methodName+"方法返回,返回值为:"+returning);

    }

    @AfterThrowing(value = "basePointCut()",throwing = "ex")
    public void doThrow(JoinPoint joinPoint,Exception ex){
        String methodName = joinPoint.getSignature().getName();
        System.out.println(methodName+"方法异常,异常信息为:"+ex.getMessage());
    }

}

We declare an basePointCut()empty method, and the return value needs to be void, the method for tagging on @Pointcutcomments, before we type an entry point that public expression of four notification method. Then, all the subsequent use to place before the cut-expression, replacing basePointCut()the method name, in order to point to the same entry point expression. All subsequent changes, I can only change basePointCut()cut expression to the method.

Published 98 original articles · won praise 13 · views 20000 +

Guess you like

Origin blog.csdn.net/qq_41885819/article/details/104772007