继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

简单模仿Struts2实现AOP

吃鸟的菜
关注TA
已关注
手记 2
粉丝 54
获赞 59

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

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

打开App,阅读手记
8人推荐
发表评论
随时随地看视频慕课网APP

热门评论

很棒,赞一个!

查看全部评论