首页 > 编程语言 >AspectJ中JoinPoint和ProceedingJoinPoint注解的使用,ProceedingJoinPoint只能用在around(环绕通知)中 环绕通知=前置+目标方法执行+后置通知,

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

时间:2022-10-18 12:44:39浏览次数:74  
标签:ProceedingJoinPoint proceed 通知 joinPoint Object 环绕 方法

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

标签:ProceedingJoinPoint,proceed,通知,joinPoint,Object,环绕,方法
From: https://www.cnblogs.com/sunny3158/p/16802208.html

相关文章

  • AOP之环绕通知 proceedingjoinpoint和前置后置通知 joinpoint区别
    AOP之proceedingjoinpoint和joinpoint区别AOP之proceedingjoinpoint和joinpoint区别现在AOP的场景越来越多,所以我们有必要理解下和AOP相关的一些概念和机制。基础知识......
  • 准考证打印通知
    考试时间:3月30日-4月1日(全国统一)准考证打印方法:湖南的考生请等学校通知。其他省市均为网上自己下载打印。可以参考下面的几张图片。公众号中发送省名即可收到报名网址。(公众......
  • Jenkins 更新通知
    JenkinsLTS2.164.1更新内容如下:Java11现已全面支持。自2.150.x开始在Java11上运行Jenkins的多项改进,包括:支持插件在它们的元数据中申明最小Java版本,并拒绝加......
  • php 微信支付V3异步回调通知 demo
    <?phpheader('Content-type:text/html;Charset=utf-8');/**请填写以下配置信息**/$publicKeyPath=getcwd().'/cert/public_key.pem';//微信支付公钥证书文......
  • shell实现接口初次失败告警,恢复也发送一次通知
    1、该shell判断第一次失败告警,接口恢复发送一次通知参数:一个参数接口返回结果0表示成功1表示失败脚本详情[root@localhostbd]#morebd-new.sh#!/bin/bashw=$(c......
  • Adobe Certified Professional 师资培训 开班通知
    为深入贯彻关于《中华人民共和国职业教育法》《中共中央国务院关于全面深化新时代教师队伍建设改革的意见》的精神,全面落实教育部办公厅发布《关于开展职业教育教师队伍能......
  • 016.五种通知类型
           ......
  • 单词本z ambition 雄心 amb = ab = about = around = 环绕
    ambition雄心amb=ab=about=around=环绕it=go=走ion名词重点是amb环绕这里是抽象含义表示内心向外扩展所以是雄心ambulance救护车amb环绕ul......
  • IDEA 插件开发(一):菜单及气泡通知
    开发工具开发工具使用IntellijIDEA,官网下载地址:https://www.jetbrains.com/idea/download/other.html推荐使用2020.3.4社区版(Community),原因如下:免费开源,在开发插件......
  • 命但如通知,但客户终每条记录能件,转换成
    命但如通知,但客户终每条记录能件,转换成F潞}命但如通知,但客户终每条记录能件,转换成m命但如通知,但客户终每条记录能件,转换成http://ds.163.com/article/63371fb8880c710001935......