当前位置: 移动技术网 > IT编程>开发语言>Java > springAOP五个通知消息调用链的原理

springAOP五个通知消息调用链的原理

2019年07月11日  | 移动技术网IT编程  | 我要评论

909ai,banacast,斗战神百味灵芝

以下是以项目的的形式就行运行验证五个消息的运行顺序及调用链的原理,里面主要用到了递归调用。

本篇博客先给大家展示代码,后面进行文字及图片讲解执行的顺序

一、创建java项目springaopmodule

二、创建项目包结构如下:

三、创建目标方法userservice

/**
 * 目标调用类
 */
public class userservice {

    public void login(string username,integer age){
        system.out.println("姓名为:"+username+",年龄为:"+age);
    }
}

  

四、创建执行接口及方法(methodinvocation/defaultmethodinvacation)

1、方法执行接口:methodinvocation

/**
 * 方法执行接口
 */
public interface methodinvocation {
    object process() throws invocationtargetexception, illegalaccessexception;
}

  

2、方法执行实习类:defaultmethodinvacation

/**
 * 执行方法实现类
 */
public class defaultmethodinvacation implements methodinvocation {
    //5个通知集合
    private list<methodinterceptor> chian;
    //目标对象
    private object target;
    private method method;// 目标方法
    private object args[];// 目标参数
    int currentchianindex;// 记录当前拦截器链调用指针

    public defaultmethodinvacation(list<methodinterceptor> chian, object target, method method, object[] args) {
        this.chian = chian;
        this.target = target;
        this.method = method;
        this.args = args;
    }

    @override
    public object process() throws invocationtargetexception, illegalaccessexception {

        if(currentchianindex==chian.size()){
            return method.invoke(target,args);
        }
        methodinterceptor methodinterceptor = chian.get(currentchianindex++);
        return methodinterceptor.invoke(this);
    }
}

  

五、创建拦截接口及方法

1、目标方法拦截接口:methodinterceptor

/**
 * 创建方法拦截接口
 */
public interface methodinterceptor {
    //执行通知的拦截
    object invoke(methodinvocation methodinvocation) throws invocationtargetexception, illegalaccessexception;
}

  2、前置通知方法

/**
 * 前置方法
 */
public class beforinterceptorimpl implements methodinterceptor {
    @override
    public object invoke(methodinvocation methodinvocation) throws invocationtargetexception, illegalaccessexception {
        system.out.println("进入前置通知");
        return  methodinvocation.process();
    }
}

  3、后置通知方法

public class aftermethodinterceptor implements methodinterceptor {
    @override
    public object invoke(methodinvocation methodinterceptor) throws invocationtargetexception, illegalaccessexception {
        object process = methodinterceptor.process();
        system.out.println("后置通知");
        return process;
    }
}

  4、环绕通知方法

public class aroundmethodinterceptor implements methodinterceptor {
    @override
    public object invoke(methodinvocation methodinvocation) throws invocationtargetexception, illegalaccessexception {
        system.out.println("环绕通知之前执行....");
        object process = methodinvocation.process();
        system.out.println("环绕通知之后执行....");
        return process;
    }
}

  六、创建测试方法execute

public class execute {
    public static void main(string[] args) throws nosuchmethodexception, invocationtargetexception, illegalaccessexception {
        arraylist<methodinterceptor> list = new arraylist<>();
        list.add(new beforinterceptorimpl());
        list.add(new aftermethodinterceptor());
        list.add(new aroundmethodinterceptor());
        userservice userservice = new userservice();
        method logingmethod = userservice.class.getmethod("login",string.class,integer.class);
        object[] objects ={"cyb",25};
        new defaultmethodinvacation(list,userservice,logingmethod,objects).process();
    }
}

  七、运行效果:

八、运行步骤原理解析:

启动运行时,系统运行步骤如下:

1、将前置通知、后置通知、环绕通知3个拦截方法放入集合中

2、获取目标方法

3、创建目标方法中的参数

4、通过有参构造方法将拦截方法集合、目标方法和参数传递到执行方法中并调用process处理方法

5、process方法中,第一次获取拦截方法中第一个前置通知,并在里面继续调用process方法,此时index下标为1,获取后置拦截方法,并在该方法中继续调用process,此时又获取的是环绕通知拦截方法,再次进入环绕通知方法,打印”环绕通知之前执行。。。”语句,此时index值为:3,再次调用process方法,此时index等于集合长度,调用模板方法,则打印目标方法语句。调完后继续打印环绕方法的”环绕通知之后执行。。。”,执行完后在方法继续返回到后置方法执行打印“后置通知”语句

九、图形讲解

注意:前置方法的打印要写在方法调用之前,后置方法打印要写在调用方法之后,环绕方法打印则分别写在调用方法前后。

以上是本人对spring aop5个通知调用链原理的讲解,大家理解后可以对这进行更好的优化,若上文有不合适的欢迎各位博友讨论。转发请说明出处,本人博客主页地址为:

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网