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 的属性值进行操作;

 

posted @ 2021-06-04 16:14  Joynic  阅读(407)  评论(0编辑  收藏  举报