一、前言
Interceptor拦截器是AOP的一种实现方式,通过拦截Action的访问,其实现原理基于java的反射机制java.lang.reflect,使用对象的代理模式(过滤器基于函数回调),下面列举和过滤器Filter几点区别,它们完整执行顺序:前置过滤器(Filter)-前置拦截器(Interceptor)- Action处理(Controller)- 后置拦截器(Interceptor) - 后置过滤器(Filter)。
1、拦截器是基于Java的反射机制,过滤器是基于java的函数回调@b@2、拦截器不依赖于servlet容器,而过滤器依赖于servlet容器@b@3、拦截器只能对action请求起作用,过滤器几乎对所有的请求起作用@b@4、拦截器可以访问action上下文,值栈里的对象,而过滤器不能访问@b@5、在action生命周期中,拦截器可以被多次调用,过滤器只能在servlet溶初始化是调用一次@b@6、拦截器可以获取IOC容器中的各个bean,过滤器不行,在拦截器中注入一个service可以调用逻辑业务
二、代码示例
下面通过完整拦截器管理完整说明实现细节及详细应用过程
/**@b@ * InterceptorManager.@b@ * 1:管理控制层、业务层全局拦截器@b@ * 2:缓存业务层 Class 级拦截器数组。在业务层仅有 injectInters、methodInters 数组未被整体缓存@b@ * 3:用于创建 Interceptor、组装 Interceptor@b@ * 4:除手动 new 出来的 inject 拦截器以外,其它所有拦截器均为单例@b@ * 5:重点关注于业务层拦截器组装性能, 控制层所有拦截器已被整体缓存@b@ * @b@ * 无法使用 Method 或 Before 对象缓存业务层 Method 级拦截器:@b@ * 1:不同对象或相同对象获取同一个 Class 中同一个 Method 得到的对象 id 值不相同@b@ * 2:不同对象获取同一个 method 之上的 Before 得到的对象 id 值不相同@b@ */@b@public class InterceptorManager {@b@ @b@ public static final Interceptor[] NULL_INTERS = new Interceptor[0];@b@ @b@ // 控制层与业务层全局拦截器@b@ private Interceptor[] globalActionInters = NULL_INTERS;@b@ private Interceptor[] globalServiceInters = NULL_INTERS;@b@ @b@ // 单例拦截器@b@ private final ConcurrentHashMap<Class<? extends Interceptor>, Interceptor> singletonMap = new ConcurrentHashMap<Class<? extends Interceptor>, Interceptor>();@b@ @b@ // 业务层 Class 级别拦截器缓存@b@ private final ConcurrentHashMap<Class<?>, Interceptor[]> serviceClassInters = new ConcurrentHashMap<Class<?>, Interceptor[]>();@b@ @b@ private static final InterceptorManager me = new InterceptorManager();@b@ @b@ private InterceptorManager() {}@b@ @b@ public static InterceptorManager me() {@b@ return me;@b@ }@b@ @b@ // 此处不缓存控制层 Class 级拦截器,已经在 com.xwood.core.Action 对象中缓存@b@ public Interceptor[] createControllerInterceptor(Class<? extends Controller> controllerClass) {@b@ return createInterceptor(controllerClass.getAnnotation(Before.class));@b@ }@b@ @b@ // 缓存业务层 Class 级拦截器@b@ public Interceptor[] createServiceInterceptor(Class<?> serviceClass) {@b@ Interceptor[] result = serviceClassInters.get(serviceClass);@b@ if (result == null) {@b@ result = createInterceptor(serviceClass.getAnnotation(Before.class));@b@ serviceClassInters.put(serviceClass, result);@b@ }@b@ return result;@b@ }@b@ @b@ public Interceptor[] buildControllerActionInterceptor(Interceptor[] injectInters, Interceptor[] classInters, Class<? extends Controller> controllerClass, Method method) {@b@ return doBuild(globalActionInters, injectInters, classInters, controllerClass, method);@b@ }@b@ @b@ public Interceptor[] buildServiceMethodInterceptor(Interceptor[] injectInters, Class<?> serviceClass, Method method) {@b@ return doBuild(globalServiceInters, injectInters, createServiceInterceptor(serviceClass), serviceClass, method);@b@ }@b@ @b@ private Interceptor[] doBuild(Interceptor[] globalInters, Interceptor[] injectInters, Interceptor[] classInters, Class<?> targetClass, Method method) {@b@ Interceptor[] methodInters = createInterceptor(method.getAnnotation(Before.class));@b@ @b@ Class<? extends Interceptor>[] clearIntersOnMethod;@b@ Clear clearOnMethod = method.getAnnotation(Clear.class);@b@ if (clearOnMethod != null) {@b@ clearIntersOnMethod = clearOnMethod.value();@b@ if (clearIntersOnMethod.length == 0) { // method 级 @Clear 且不带参@b@ return methodInters;@b@ }@b@ } else {@b@ clearIntersOnMethod = null;@b@ }@b@ @b@ Class<? extends Interceptor>[] clearIntersOnClass;@b@ Clear clearOnClass = targetClass.getAnnotation(Clear.class);@b@ if (clearOnClass != null) {@b@ clearIntersOnClass = clearOnClass.value();@b@ if (clearIntersOnClass.length == 0) { // class 级 @clear 且不带参@b@ globalInters = NULL_INTERS;@b@ injectInters = NULL_INTERS;@b@ }@b@ } else {@b@ clearIntersOnClass = null;@b@ }@b@ @b@ ArrayList<Interceptor> result = new ArrayList<Interceptor>(globalInters.length + injectInters.length + classInters.length + methodInters.length);@b@ for (Interceptor inter : globalInters) {@b@ result.add(inter);@b@ }@b@ for (Interceptor inter : injectInters) {@b@ result.add(inter);@b@ }@b@ if (clearIntersOnClass != null && clearIntersOnClass.length > 0) {@b@ removeInterceptor(result, clearIntersOnClass);@b@ }@b@ for (Interceptor inter : classInters) {@b@ result.add(inter);@b@ }@b@ if (clearIntersOnMethod != null && clearIntersOnMethod.length > 0) {@b@ removeInterceptor(result, clearIntersOnMethod);@b@ }@b@ for (Interceptor inter : methodInters) {@b@ result.add(inter);@b@ }@b@ return result.toArray(new Interceptor[result.size()]);@b@ }@b@ @b@ private void removeInterceptor(ArrayList<Interceptor> target, Class<? extends Interceptor>[] clearInters) {@b@ for (Iterator<Interceptor> it = target.iterator(); it.hasNext();) {@b@ Interceptor curInter = it.next();@b@ if (curInter != null) {@b@ Class<? extends Interceptor> curInterClass = curInter.getClass();@b@ for (Class<? extends Interceptor> ci : clearInters) {@b@ if (curInterClass == ci) {@b@ it.remove();@b@ break;@b@ }@b@ }@b@ } else {@b@ it.remove();@b@ }@b@ }@b@ }@b@ @b@ public Interceptor[] createInterceptor(Before beforeAnnotation) {@b@ if (beforeAnnotation == null) {@b@ return NULL_INTERS;@b@ }@b@ return createInterceptor(beforeAnnotation.value());@b@ }@b@ @b@ public Interceptor[] createInterceptor(Class<? extends Interceptor>[] interceptorClasses) {@b@ if (interceptorClasses == null || interceptorClasses.length == 0) {@b@ return NULL_INTERS;@b@ }@b@ @b@ Interceptor[] result = new Interceptor[interceptorClasses.length];@b@ try {@b@ for (int i=0; i<result.length; i++) {@b@ result[i] = singletonMap.get(interceptorClasses[i]);@b@ if (result[i] == null) {@b@ result[i] = (Interceptor)interceptorClasses[i].newInstance();@b@ singletonMap.put(interceptorClasses[i], result[i]);@b@ }@b@ }@b@ return result;@b@ } catch (Exception e) {@b@ throw new RuntimeException(e);@b@ }@b@ }@b@ @b@ public void addGlobalActionInterceptor(Interceptor... inters) {@b@ addGlobalInterceptor(true, inters);@b@ }@b@ @b@ public void addGlobalServiceInterceptor(Interceptor... inters) {@b@ addGlobalInterceptor(false, inters);@b@ }@b@ @b@ private void addGlobalInterceptor(boolean forAction, Interceptor... inters) {@b@ if (inters == null || inters.length == 0) {@b@ throw new IllegalArgumentException("interceptors can not be null.");@b@ }@b@ @b@ for (Interceptor inter : inters) {@b@ if (inter == null) {@b@ throw new IllegalArgumentException("interceptor can not be null.");@b@ }@b@ if (singletonMap.containsKey(inter.getClass())) {@b@ throw new IllegalArgumentException("interceptor already exists, interceptor must be singlton, do not create more then one instance of the same Interceptor Class.");@b@ }@b@ }@b@ @b@ for (Interceptor inter : inters) {@b@ singletonMap.put(inter.getClass(), inter);@b@ }@b@ @b@ Interceptor[] globalInters = forAction ? globalActionInters : globalServiceInters;@b@ Interceptor[] temp = new Interceptor[globalInters.length + inters.length];@b@ System.arraycopy(globalInters, 0, temp, 0, globalInters.length);@b@ System.arraycopy(inters, 0, temp, globalInters.length, inters.length);@b@ @b@ if (forAction) {@b@ globalActionInters = temp;@b@ } else {@b@ globalServiceInters = temp;@b@ }@b@ }@b@}