提问者:小点点

Spring AOP中use@PointCut方法的含义是什么,只是一个切入点签名?


学习Spring AOP,代码如下:

@Component
@Aspect
public class FanAnnotationImpl {

    @Pointcut("@annotation(com.fan.spboot.core.aopdemo.FanAnnotation)")
    private void entry(){
        System.out.println("entry annotation");
    }
    @Around("entry()")
    public void around(ProceedingJoinPoint joinPoint)throws Throwable{
        System.out.println("around before");
        try {
            joinPoint.proceed();
        }catch (Exception e){
            e.printStackTrace();
        }
        System.out.println("around after");
    }

    @Before("entry()")
    public void before(){

        System.out.println("Before entry");

    }

    @After("entry()")
    public void after(){
        System.out.println("After entry");

    }
}

spring-aop-point Cut-教程有一个介绍:

“方法声明称为切入点签名。它提供了一个名称,通知注释可以使用该名称来引用该切入点。”

让我感到困惑的是方法use@PointCut,它只是一个切入点签名?因为我发现这个方法中的代码没有执行,改变这个方法的类型就可以了;那为什么是方法?使用变量也可以吗?


共1个答案

匿名用户

你自己已经引用了手册。很清楚,不是吗?Spring AOP基于注释,而不是变量。注释是Java向类、方法或其他语言元素添加信息的标准方法。

方法只是定义可在多个位置使用的切入点的一种方法,例如,如果您想要组合多个切入点,如切入点1

@PointCut注释的方法当然永远不会被Spring AOP调用,因为该方法只是您需要通过切入点注释来装饰的假人。毕竟,您需要将注释放在某个地方。

如果您只在单个位置使用切入点,则无需通过@PointCut定义它,您可以直接将切入点写入您的@前@后@周围注释中。

实际上,这个答案非常多余,因为Spring AOP手册中对所有内容都进行了很好的解释。