环绕通知=前置+目标方法执行+后置通知
业务背景:需要先去redis操作数据,再判断要不要去关系型数据库操作数据
先自定义一个注解(如果有其他需求可以自己加参数),用来做标识以及环绕通知的切入点。
@Around注解的使用
/*一、
可自定义切点位置,针对不同切点,方法上的@Around()可以这样写ex:@Around(value = "methodPointcut() && args(..)")
@Pointcut(value = "@annotation(com.rq.aop.common.annotation.MyAnnotation)")
public void methodPointcut(){}
@Pointcut(value = "@annotation(com.rq.aop.common.annotation.MyAnnotation2)")
public void methodPointcut2(){}
*/
/*二、
使用自定义注解
@Around(value = "@annotation(redisCacheAnnotation)")
此注解要与下方法参数中的注解参数名名字相同
*/
ProceedingJoinPoint的使用
MethodSignature methodSignature =(methodSignature )joinPoint.getSignature()
获取增强方法方法名: proceedingjoinPoint.getSignature().getName()
获取增强方法上的注解:methodSignature.getMethod().getAnnotation(xxx.class)
执行增强方法:proceedingJoinPoint.proceed()
小结:
环绕通知是AOP方法里面最常用也是最强大的方法,配合ProceedingJoinPoint可以在增强类里面实现任何你想实现的