1

learning the Spring AOP, the code like this:

@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");

    }
}

the spring-aop-pointcut-tutorial has a introduction:

"The method declaration is called the pointcut signature. It provides a name that can be used by advice annotations to refer to that pointcut."

What make me feel puzzled is the method use @Pointcut ,it's just a pointcut signature? Because I find the code in this method not executed, and change the type of this method is OK; Then why is a method? Use a variable is also OK?

Fanl
  • 1,491
  • 2
  • 11
  • 15

1 Answers1

2

You already quoted the manual yourself. It is pretty clear, is it not? Spring AOP is based on annotations, not on variables. Annotations are a standard way in Java to add information to classes, methods or other language elements.

A @Pointcut method is only a way to define pointcuts which can be used in multiple places, e.g. if you want to combine multiple pointcuts like pointcut1 && (pointcut2 || pointcut3) or just use the same pointcut in multiple advice methods. It is a way for you as a developer not to have to repeat yourself and write the same pointcut many times. Another advantage is that you can modify the pointcut in one place and it gets updated everywhere it is used.

The method annotated by @Pointcut of course is never called by Spring AOP because the method is only a dummy you need to get decorated by the pointcut annotation. You need to put the annotation somewhere, after all.

If you use your pointcut in only a single place, there is no need to define it via @Pointcut, you can just write the pointcut directly into your @Before, @After or @Around annotation.

Actually this answer is pretty much superfluous because everything is explained well in the Spring AOP manual.

halfer
  • 19,824
  • 17
  • 99
  • 186
kriegaex
  • 63,017
  • 15
  • 111
  • 202
  • I think OP is asking: Why can't we simply use string variables to store pointcut expressions? Why does Spring have to achieve this with methods? – Raymond Pang Jun 14 '22 at 15:25
  • 1
    I answered that question in the first paragraph. I would have to dive deeper into how the native AspectJ compiler/weaver works in order to explain the details. Suffice to say that Spring borrows AOP syntax from AspectJ and utilises its pointcut parser and matcher in order to implement its own, proxy-based "AOP lite" framework in order to spare users having to learn two AOP dialects. – kriegaex Jun 15 '22 at 18:42
  • 1
    If you want to manully create aspects from building blocks in Spring, you can also use variables. But IMO that is tedious and rather for exotic cases - or for masochists. Please refer to [my answer here](https://stackoverflow.com/q/69207046/1082681) for a little example of how to create an old-fashioned `Advisor` with an `AspectJExpressionPointcut` the value of which you can set via its `setExpression` method. But why bother if both AspectJ and Spring AOP make it so easy for you to create aspects? – kriegaex Jun 15 '22 at 18:44