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
测试成功,收工,大家一起加油。
热门评论
很棒,赞一个!