sunny123456

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

AspectJ中JoinPoint和ProceedingJoinPoint注解的使用,ProceedingJoinPoint只能用在around(环绕通知)中
环绕通知=前置+目标方法执行+后置通知,proceed方法就是用于启动目标方法执行的

概念

Joint Point

JointPoint是程序运行过程中可识别的点,这个点可以用来作为AOP切入点。JointPoint对象则包含了和切入相关的很多信息。比如切入点的对象,方法,属性等。我们可以通过反射的方式获取这些点的状态和信息,用于追踪tracing和记录logging应用信息。

Pointcut

pointcut 是一种程序结构和规则,它用于选取join point并收集这些point的上下文信息。
pointcut通常包含了一系列的Joint Point,我们可以通过pointcut来同时操作jointpoint。单从概念上,可以把Pointcut当做jointpoint的集合。

JointPoint和ProceedingJoinPoint区别
JoinPoint
joinpoint的方法如下

# 返回目标对象,即被代理的对象
Object getTarget();
# 返回切入点的参数
Object[] getArgs();
# 返回切入点的Signature
Signature getSignature();
# 返回切入的类型,比如method-call,field-get等等,不重要
 String getKind();
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

ProceedingJoinPoint
Proceedingjoinpoint 继承了 JoinPoint。是在JoinPoint的基础上暴露出 proceed 这个方法。proceed很重要,这个是aop代理链执行的方法。

public interface ProceedingJoinPoint extends JoinPoint {
    void set$AroundClosure(AroundClosure var1);
    default void stack$AroundClosure(AroundClosure arc) {
        throw new UnsupportedOperationException();
    }
    Object proceed() throws Throwable;
    Object proceed(Object[] var1) throws Throwable;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

ProceedingJoinPoint只能用在around(环绕通知)中
环绕通知=前置+目标方法执行+后置通知,proceed方法就是用于启动目标方法执行的

joinPoint使用方法如下

		//1.获取切入点所在的目标对象
       	Object obj = joinPoint.getTarget();  
       	//2.获取修饰符+ 包名+组件名(类名) +方法名
        Signature signature = joinPoint.getSignature();
		//3.获取方法上的注解
		MethodSignature methodSignature = (MethodSignature) signature;
        Method method = methodSignature.getMethod();
        if (method != null)
        {
            xxxxxx annoObj= method.getAnnotation(xxxxxx.class);
        }
        return null;
        //4.获取切入点方法上的参数列表
        Object[] args = joinPoint.getArgs();
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

使用案例,环绕通知
1.定义一个注解 模拟记录日志

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface CustomLog {
    String title() default "";
}
  • 1
  • 2
  • 3
  • 4
  • 5

2.定一个切面

@Aspect
@Component
@Slf4j
public class CustomAspect {
    /**
     * 定义切入点
     */
    @Pointcut("@annotation(io.renren.common.annotation.CustomLog)")
    public void CusjoinPoint() {
    }
    /**
     * 环绕通知
     * @param joinPoint
     * @return
     * @throws Throwable
     */
    @Around("CusjoinPoint()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        Object proceed = joinPoint.proceed();
        log(joinPoint);
        return proceed;
    }
    /**
     * 简单打印日志--
     * @param joinPoint
     */
    private void log(ProceedingJoinPoint joinPoint) {
        Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        Method method = methodSignature.getMethod();
        String methodName = method.getName();
        CustomLog customLog = method.getDeclaredAnnotation(CustomLog.class);
        String title = customLog.title();
        log.info("methodName =>{},title=>>>>{}",methodName,title);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40

3.把注解打到接口上面,然后调用该接口可以看到打印的日志

https://blog.csdn.net/Erica_java/article/details/124627180
posted on 2022-10-18 12:35  sunny123456  阅读(697)  评论(0编辑  收藏  举报