转自 http://www.blogjava.net/atealxt/archive/2009/09/20/spring_mvc_annotation_interceptor_gae.html
Spring-MVC如何使用拦截器,官方文档只给出了非注解风格的例子。那么基于注解风格如何使用拦截器呢?
基于注解基本上有2个可使用的定义类,分别是DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter
- < bean class = "org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
- < bean class = "org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
1、DefaultAnnotationHandlerMapping
DefaultAnnotationHandlerMapping本身支持自定义拦截器,只需按如下进行配置:
- < bean class = "org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" >
- < property name = "interceptors" >
- < list >
- < bean class = "packageName.XXXInterceptor" />
- </ list >
- </ property >
- </ bean >
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="interceptors">
<list>
<bean class="packageName.XXXInterceptor" />
</list>
</property>
</bean>
Interceptor的定义为:
- public class XXXInterceptor extends HandlerInterceptorAdapter {
- @Override
- public boolean preHandle(HttpServletRequest req, HttpServletResponse resp, Object handler) {
-
- String className = handler.getClass().getName();
- if (Error) {
- return false ;
- }
- return true ;
- }
- }
public class XXXInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest req, HttpServletResponse resp, Object handler) {
String className = handler.getClass().getName();// packageName.ClassName
if (Error) {
return false;
}
return true;
}
}
2、AnnotationMethodHandlerAdapter
目前,笔者没找到如何给AnnotationMethodHandlerAdapter配置自定义Interceptor的方法,但是有个customArgumentResolver可以利用一下,来充当Interceptor。
- < bean class = "org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" >
- < property name = "customArgumentResolver" >
- < bean class = "packageName.XXXResolver" />
- </ property >
- </ bean >
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="customArgumentResolver">
<bean class="packageName.XXXResolver"/>
</property>
</bean>
Resolver的定义为:
- public class XXXResolver implements WebArgumentResolver {
-
- @Override
- public Object resolveArgument(MethodParameter methodParameter, NativeWebRequest webRequest) throws Exception {
-
- String className = methodParameter.getMethod().getDeclaringClass().getName();
-
-
- HttpServletResponse resp = (HttpServletResponse) webRequest.getNativeResponse();
- HttpServletRequest req = (HttpServletRequest) webRequest.getNativeRequest();
-
- if (Error) {
- if (!resp.isCommitted()) resp.sendError(ERROR_STATUS);
- }
- return UNRESOLVED;
- }
- }
public class XXXResolver implements WebArgumentResolver {
@Override
public Object resolveArgument(MethodParameter methodParameter, NativeWebRequest webRequest) throws Exception {
String className = methodParameter.getMethod().getDeclaringClass().getName();// packageName.ClassName
// 如何取得Response和Request
HttpServletResponse resp = (HttpServletResponse) webRequest.getNativeResponse();
HttpServletRequest req = (HttpServletRequest) webRequest.getNativeRequest();
if (Error) {
if (!resp.isCommitted()) resp.sendError(ERROR_STATUS);
}
return UNRESOLVED;
}
}
仔细的人会看出,第二种方法其实根本不是拦截。其实第二种只是在映射Controller,调用方法的时候,给每一个方法的参数增加了一个切点。
上例在出错的时候往HttpServletResponse 写错误状态,来通知web容器进行错误重定向,达到了拦截器的作用。
这么做有一个缺点,就是每个参数都有自己的切点,比如方法有3个参数就会调3次resolveArgument。为了避免出错,需要判断一下 resp.isCommitted 。
customArgumentResolver的初衷不是用来做Interceptor的,但有些环境却不得不使用它,比如部署在GAE上。
GAE 是不支持DefaultAnnotationHandlerMapping的,因为此类用到了 org.springframework.beans.BeanUtils.findEditorByConvention,这个方法会调用 java.lang.ClassLoader.getSystemClassLoader,而这正是GAE所不允许的。
PS:
文中提到软件的版本:
spring - 2.5.X
google app engine - 1.2.5
参考资料
http://www.scottmurphy.info/spring_framework_annotation_based_controller_interceptors
http://groups.google.com/group/google-appengine-java/browse_thread/thread/54aadf8164be7bf0