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:
- 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");
}
}