How to use interceptors in annotation-style SPRING-MVC

Source: Internet
Author: User

Spring-mvc How to use interceptors, official documents give only examples of non-annotation styles. So how do you use interceptors based on the annotation style?

Based on annotations There are basically 2 defined classes that can be used, namely defaultannotationhandlermapping and Annotationmethodhandleradapter:

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>

1、DefaultAnnotationHandlerMapping

The defaultannotationhandlermapping itself supports custom interceptors by simply configuring them as follows:

1 <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
2     <property name="interceptors">
3         <list>
4                <bean class="packageName.XXXInterceptor" />
5         </list>
6     </property>
7 </bean>

The definition of interceptor is:

 1 public class XXXInterceptor extends HandlerInterceptorAdapter {
2     @Override
3     public boolean preHandle(HttpServletRequest req, HttpServletResponse resp, Object handler) {
4
5         String className = handler.getClass().getName();// packageName.ClassName
6         if (Error) {
7             return false;
8         }
9         return true;
10     }
11 }

2, Annotationmethodhandleradapter

At present, the author did not find how to configure a custom interceptor for annotationmethodhandleradapter, but there is a customargumentresolver can be used to act as a interceptor.

1 <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
2     <property name="customArgumentResolver">
3         <bean class="packageName.XXXResolver"/>
4     </property>
5 </bean>

The definition of resolver is:

 1 public class XXXResolver implements WebArgumentResolver {
2
3     @Override
4     public Object resolveArgument(MethodParameter methodParameter, NativeWebRequest webRequest) throws Exception {
5
6         String className = methodParameter.getMethod().getDeclaringClass().getName();// packageName.ClassName
7
8         // 如何取得Response和Request
9         HttpServletResponse resp = (HttpServletResponse) webRequest.getNativeResponse();
10        HttpServletRequest req = (HttpServletRequest) webRequest.getNativeRequest();
11
12        if (Error) {
13            if (!resp.isCommitted()) resp.sendError(ERROR_STATUS);
14        }
15        return UNRESOLVED;
16     }
17 }
18

A careful person will see that the second method is actually not interception at all. In fact, the second way is to add a tangent point to each method's arguments when mapping controller and calling methods.

The above example writes the error state to the HttpServletResponse when the error occurs, notifies the Web container to make error redirection, and achieves the function of the interceptor.

The disadvantage of doing so is that each parameter has its own pointcut, for example, 3 parameters of the method will be tuned 3 times resolveargument. To avoid mistakes, you need to judge resp.iscommitted.

Customargumentresolver's original intention was not to do interceptor, but some environments had to use it, such as deploying it on Gae.

Gae is not supported by Defaultannotationhandlermapping, Because this class uses org.springframework.beans.BeanUtils.findEditorByConvention, this method calls Java.lang.ClassLoader.getSystemClassLoader, which is exactly what G Not allowed by AE.

Ps:

The software version is mentioned in the article:

spring-2.5.x

Google app engine-1.2.5

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.