转自:http://rejoy.iteye.com/blog/1881180
在Struts2中有拦截器的概念,通过它的拦截器可以拦截Action。Struts2的拦截器是通过AOP来实现的,在Spring也有类似的概念。下面的我们先来比较一下Struts2和Spring中AOP的东西。
AOP概念 | Struts2 | Spring |
JoinPoint | Action中方法的执行 | 符合条件方法的执行 |
Pointcut | Action(不能自己指定) | 可以通过正则或AspectJ表达式来指定 |
Advice | Before、After、Around | Before、After、Around |
Aspect | 拦截器 | 拦截器 |
从上面的比较中可以看到,Struts2的AOP功能比较单一,只能拦截Action类中的方法。Spring的AOP是通过JDK动态代理或者CGLib来生成目标对象的代理对象,然后将增强功能(Aspect【包括了Advice和Pointcut】)织入到符合条件(Pointcut)的类的方法(JoinPoint)上。
Struts2的AOP实现跟Filter的实现差不多,它有一系列的拦截器,称为拦截器栈,通过这些拦截器栈通过ActionInvocation的调度可以在Action中方法执行之前或执行做一些操作。原理图如下:
下面,用代码来模拟一下它的实现
1.Action 接口
public interface Action {
String execute();
}
2.Interceptor接口
public interface Interceptor {
String intercept(ActionInvocation invocation);
}
3.ActionInvocation接口
public interface ActionInvocation {
String invoke();
}
4.调度器实现
import java.util.ArrayList;
import java.util.List;
public class DefaultActionInvocation implements ActionInvocation {
private int index;
private Action action;
private List<Interceptor> interceptors = new ArrayList<Interceptor>();
public void addInterceptor(Interceptor interceptor) {
this.interceptors.add(interceptor);
}
public void setAction(Action action) {
this.action = action;
}
@Override
public String invoke() {
String result = "";
if (index == interceptors.size()) {
result = this.action.execute();
} else {
Interceptor interceptor = interceptors.get(index);
index++;
System.out.println("拦截前:" + interceptor.getClass().getName());
result = interceptor.intercept(this);
System.out.println("拦截后:" + interceptor.getClass().getName());
}
return result;
}
}
5.拦截器实现
1).环绕通知拦截器
public class AroundInterceptor implements Interceptor {
@Override
public String intercept(ActionInvocation invocation) {
System.out.println("AroundInterceptor拦截前通知:" + this.getClass());
String result = invocation.invoke();
System.out.println("AroundInterceptor拦截后通知:" + this.getClass());
return result;
}
}
2).异常通知拦截器
public class ExceptionInterceptor implements Interceptor {
@Override
public String intercept(ActionInvocation invocation) {
System.out.println("ExceptionInterceptor拦截");
return invocation.invoke();
}
}
3).I18N烂机器
public class I18NInterceptor implements Interceptor {
@Override
public String intercept(ActionInvocation invocation) {
System.out.println("I18NInterceptor拦截");
return invocation.invoke();
}
}
4).其他......
6.Action实现
public class MyAction implements Action {
@Override
public String execute() {
System.out.println("execute...");
return "success:" + getClass();
}
}
7.测试调用
public class Test {
public static void main(String[] args) {
Interceptor exptionInterceptor = new ExceptionInterceptor();
Interceptor i18nInterceptor = new I18NInterceptor();
Interceptor aroundInterceptor = new AroundInterceptor();
DefaultActionInvocation actionInvocation = new DefaultActionInvocation();
actionInvocation.addInterceptor(exptionInterceptor);
actionInvocation.addInterceptor(i18nInterceptor);
actionInvocation.addInterceptor(aroundInterceptor);
Action action = new MyAction();
actionInvocation.setAction(action);
String result = actionInvocation.invoke();
System.out.println("Action 结果:" + result);
}
}
测试结果:
拦截前:ExceptionInterceptor
ExceptionInterceptor拦截
拦截前:I18NInterceptor
I18NInterceptor拦截
拦截前:AroundInterceptor
AroundInterceptor拦截前通知:class AroundInterceptor
execute...
AroundInterceptor拦截后通知:class AroundInterceptor
拦截后:AroundInterceptor
拦截后:I18NInterceptor
拦截后:ExceptionInterceptor
Action 结果:success:class MyAction
可以看到添加进去的Interceptor像是一个栈一样,如果放进去的的Interceptor是BeforeAdvice类型的,则会在Action中的方法执行之前,执行增强操作;如果放进去的Interceptor是AroundAdvice类型的,则会在Action中的方法之前按照“进栈”的顺序执行增强操作,在Action方法执行之后,按照“出栈”的顺序执行增强操作
(1)拦截前:ExceptionInterceptor
ExceptionInterceptor拦截
(2)拦截前:I18NInterceptor
I18NInterceptor拦截
(3)拦截前:AroundInterceptor
AroundInterceptor拦截前通知:class AroundInterceptor
(4)execute...
AroundInterceptor拦截后通知:class AroundInterceptor
(3)拦截后:AroundInterceptor
(2)拦截后:I18NInterceptor
(1)拦截后:ExceptionInterceptor
Action 结果:success:class MyAction