为了账号安全,请及时绑定邮箱和手机立即绑定

简单模仿Struts2实现AOP

标签:
Java

Struts2非常巧妙地利用递归算法来实现AOP(Aspect Oriented Programming,面向切面编程),我们来简单模仿一下其执行流程。
Action接口:

public interface Action {
    String execute();
}

MyAction实现Action:

public class MyAction implements Action {
    @Override
    public String execute() {
        System.out.println("=====执行Action=====");
        return "success";
    }
}

Interceptor接口:

public interface Interceptor {
    String intecept(ActionInvocation invocation);
}

下面分别是Interceptor的三个实现类:

public class TimerInterceptor implements Interceptor{
    @Override
    public String intecept(ActionInvocation invocation) {
        System.out.println("=====1.执行TimerInterceptor");
        String result = invocation.invoke();
        System.out.println("=====1.结束执行TimerInterceptor");
        return result;
    }
}

public class LoggingInterceptor implements Interceptor{
    @Override
    public String intecept(ActionInvocation invocation) {
        System.out.println("=====2.执行LoggingInterceptor");
        String result = invocation.invoke();
        System.out.println("=====2.结束执行LoggingInterceptor");
        return result;
    }
}

public class ChainInterceptor implements Interceptor{
    @Override
    public String intecept(ActionInvocation invocation) {
        System.out.println("=====3.执行ChainInterceptor");
        String result = invocation.invoke();
        System.out.println("=====3.结束执行ChainInterceptor");
        return result;
    }
}

下面就是我们的总指挥ActionInvocation接口了:

public interface ActionInvocation {
    String invoke();
}

ActionInvocation的默认实现类DefaultActionInvocation,所有的调度都是在这里完成的:

public class DefaultActionInvocation implements ActionInvocation{
    private int count=0;    //定义一个数字来判断interceptors集合是否被执行完
    private Action action;
    private List<Interceptor> interceptors;
    public DefaultActionInvocation() {
        interceptors=new ArrayList<Interceptor>();
    }
    @Override
    public String invoke() {
        String result=null;
        if(count<interceptors.size()){  //如果interceptors集合没有执行完,继续执行下一个interceptor
            /*这步是递归的关键,intercept方法中会调用ActionInvocation的invoke方法,形成递归调用*/
            result=interceptors.get(count++).intecept(this);
        }else if(count==interceptors.size()){   //interceptors集合执行完毕
            result=action.execute();
        }
        return result;
    }
    //设置Action
    public void setAction(Action action){
        this.action=action;
    }
    //添加interceptor
    public void addInterceptor(Interceptor interceptor){
        interceptors.add(interceptor);
    }
}

好了,该测试一下了,看看效果,测试代码如下:

public class MyTestAOP {
    public static void main(String[] args) {
        DefaultActionInvocation actionInvocation=new DefaultActionInvocation();

        Interceptor timerInterceptor=new TimerInterceptor();
        Interceptor loggingInterceptor=new LoggingInterceptor();
        Interceptor chainInteceptor=new ChainInterceptor();

        Action action=new MyAction();
        //添加拦截器
        actionInvocation.addInterceptor(timerInterceptor);
        actionInvocation.addInterceptor(loggingInterceptor);
        actionInvocation.addInterceptor(chainInteceptor);
        //设置Action
        actionInvocation.setAction(action);
        //总指挥,发威吧
        actionInvocation.invoke();
    }
}

犹抱琵琶半遮面的测试结果露面了:

=====1.执行TimerInterceptor
=====2.执行LoggingInterceptor
=====3.执行ChainInterceptor
=====执行Action=====
=====3.结束执行ChainInterceptor
=====2.结束执行LoggingInterceptor
=====1.结束执行TimerInterceptor

测试成功,收工,大家一起加油。

点击查看更多内容
8人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消