SpringBoot @Aspect编程
定义aspect 类:
@Aspect @Component public class ExampleAspect { }
拦截指定annotation:
@Around("@annotation(LogExecutionTime)") public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable { return joinPoint.proceed(); }
在 around 方法中获取被拦截的方法:
Signature signature = proceedingJoinPoint.getSignature(); MethodSignature methodSignature = (MethodSignature) signature; Method method = methodSignature.getMethod();
可以操作从 method 上获取annotation:
method.getAnnotation(LogExecutionTime.class);
取得annotation 的属性值进行操作;