Failure of Spring Boot AOP comment interception

expectation: define an annotation yourself using SpringBoot + AOP, and intercept the method of placing annotations through AOP.
has a problem: it can be intercepted when annotations are placed on methods with controller entry annotations such as GetMapping, but not when annotations are liberated to other methods.

main code:

  1. comments
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Lock {
}

2. Section

@Aspect
@Component
public class HelloAspect {


    @Around("@annotation(myLock)")
    public Object around(ProceedingJoinPoint pjp, Lock myLock) {
        //
        System.out.println("in around");
        try {
            return pjp.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            return null;
        }
    }


}

3.Controller

@RestController
public class HelloController {


    // 
    @GetMapping("/hello2")
    @Lock
    public String hello2() {
        return "hello2";
    }

    @GetMapping("/hello")
    public String hello() {
        a();
        return "hello";
    }

    // 
    @Lock
    protected void a(){
        System.out.println("aaa");
    }


}
Mar.13,2021

@Autowired
private HelloController helloController;
helloController.a()

helloController is a proxy object, so calling AOP will take effect.
calling the a () method directly is the current object call, not a proxy object, so it is invalid

.

should be something annotated by @ RestController. You can use annotations such as @ Service to try

.
Menu