AspectJ中JoinPoint和ProceedingJoinPoint注解的使用

概念

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();

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;
}

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.定义一个注解 模拟记录日志

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface CustomLog {
    String title() default "";
}

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

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

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值