什么';s在Spring AOP中使用@Pointcut的方法的含义,;只是一个切入点的签名?
学习Spring AOP时,代码如下:什么';s在Spring AOP中使用@Pointcut的方法的含义,;只是一个切入点的签名?,spring,aspectj,spring-aop,Spring,Aspectj,Spring Aop,学习Spring AOP时,代码如下: @Component @Aspect public class FanAnnotationImpl { @Pointcut("@annotation(com.fan.spboot.core.aopdemo.FanAnnotation)") private void entry(){ System.out.println("entry annotation"); } @
@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切入点教程有一个介绍:
“该方法声明称为切入点签名。它提供了一个名称,可供通知批注用于引用该切入点。”
让我感到困惑的是使用@Pointcut的方法,它只是一个切入点签名?
因为我发现这个方法中的代码没有执行,改变这个方法的类型是可以的;
那为什么是一种方法呢?使用变量也可以吗?您自己已经引用了手册。这很清楚,不是吗?SpringAOP基于注释,而不是变量。注释是Java中向类、方法或其他语言元素添加信息的标准方式
@Pointcut
方法只是定义可在多个位置使用的切入点的一种方法,例如,如果您想要组合多个切入点,如pointcut1&(pointcut2 | | pointcut3)
,或者只在多个通知方法中使用相同的切入点。作为开发人员,这是一种不必重复自己并多次编写相同切入点的方法。另一个优点是,您可以在一个地方修改切入点,并且它在使用的任何地方都会得到更新
当然,@Pointcut
注释的方法永远不会被Spring AOP调用,因为该方法只是一个伪方法,您需要通过切入点注释进行修饰。毕竟,您需要将注释放在某个地方
如果您仅在一个位置使用切入点,则无需通过@pointcut
定义切入点,您只需将切入点直接写入@Before
、@After
或@about
注释中即可
实际上,这个答案是多余的,因为Spring AOP手册对所有内容都做了很好的解释。请提供反馈。非常感谢。