SpringMVC interceptor interceptors use execution order appreciated SpringMVC

           SpringMVC interceptor interceptors use execution order appreciated SpringMVC

 

First, the interceptor role

1, Spring Web MVC processor interceptors like filter Filter Servlet Development, processor for preprocessing and postprocessing.

 

Second, the use

1, rely on spring-webmvc

 <dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-webmvc</artifactId>
	    <version>4.3.20.RELEASE</version>
     </dependency>

 

2, LogInterceptor create a class that implements org.springframework.web.servlet.HandlerInterceptor Interface

 

3, rewrite preHandle, postHandle, afterCompletion action follows :

 /**
	 * preHandle: Controller 执行之前,调用该方法
	 * 返回 true,表示继续执行 ; 返回 false ,终止执行。
	 * 应用: 登陆校验、权限拦截 等。
	 */
	@Override
	public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
		System.out.println("LogInterceptor =====> preHandle ");
		return true;
	}
	
	/**
	 * postHandle: Controller 执行之后,但未返回视图前,调用此方法。
	 * 应用:对模型数据进行加工处理,加入公用信息以便页面显示 ; 或者手机、平板等移动端访问,返回对应页面数据。
	 */
	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView view)
			throws Exception {
		System.out.println("LogInterceptor =====> postHandle ");
		if(null != view) {
			System.out.println("viewName: "+view.getViewName());
		}
		
	}
	
	/**
	 * afterCompletion: Controller 执行之后,且返回视图后,调用此方法。
	 * 应用:日志记录、异常记录、资源清理等。
	 */
	@Override
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		System.out.println("LogInterceptor =====> afterCompletion ");
	}

 

4, spirng-mvc.xml configuration interceptors

<mvc:interceptors>
    <!-- LogInterceptor  -->
    <mvc:interceptor>
        <mvc:mapping path="/**"/>
        <bean id="logInterceptor" class="com.runcode.interceptor.LogInterceptor" />
    </mvc:interceptor>
  </mvc:interceptors>

 

5, start the project, any request for a url, you can see the console output:

LogInterceptor =====> preHandle  //  执行Controller 之前
hello  // 执行Controller
LogInterceptor =====> postHandle // 执行Controller,返回视图之前 
LogInterceptor =====> afterCompletion // 执行Controller,返回视图之后

 

Third, understand the execution order

1, respectively, to create FirstInterceptor and SecondInterceptor ( source here )

public class FirstInterceptor implements HandlerInterceptor {
	
	@Override
	public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
		System.out.println("FirstInterceptor  ---> preHandle" );
		return true;
	}
	
	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {
		System.out.println("FirstInterceptor  ---> postHandle" );
	}
	
	@Override
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		System.out.println("FirstInterceptor  ---> afterCompletion" );
	}
}

 

2, FirstInterceptor and SecondInterceptor sequentially arranged as follows (First First, after the Second):

<mvc:interceptors>
		<mvc:interceptor>
			<mvc:mapping path="/userController/**"/>
			<bean class="com.runcode.interceptor.FirstInterceptor" />
		</mvc:interceptor>
		<mvc:interceptor>
			<mvc:mapping path="/userController/**"/>
			<bean class="com.runcode.interceptor.SecondInterceptor" />
		</mvc:interceptor>
	</mvc:interceptors>

3, FirstInterceptor and SecondInterceptor of preHandle method returns true

4, the console output is as follows:

FirstInterceptor  ---> preHandle
SecondInterceptor  ---> preHandle
SecondInterceptor  ---> postHandle
FirstInterceptor  ---> postHandle
SecondInterceptor  ---> afterCompletion
FirstInterceptor  ---> afterCompletion

 

5, SecondInterceptor and FirstInterceptor reverse configuration is as follows (First Second, after First)

<mvc:interceptors>
		<mvc:interceptor>
			<mvc:mapping path="/userController/**"/>
			<bean class="com.runcode.interceptor.SecondInterceptor" />
		</mvc:interceptor>
		<mvc:interceptor>
			<mvc:mapping path="/userController/**"/>
			<bean class="com.runcode.interceptor.FirstInterceptor" />
		</mvc:interceptor>
	</mvc:interceptors>

6, FirstInterceptor and SecondInterceptor of preHandle method returns true.

7, the console output:

SecondInterceptor  ---> preHandle
FirstInterceptor  ---> preHandle
FirstInterceptor  ---> postHandle
SecondInterceptor  ---> postHandle
FirstInterceptor  ---> afterCompletion
SecondInterceptor  ---> afterCompletion

8, according to the order of arrangement interceptors, disposed on the front: The preHandle first implementation; afterCompletion The postHandle and, after execution.

 

Fourth, return false test

1, FirstInterceptor SecondInterceptor sequence and configuration, but FirstInterceptor the preHandle returns false, the return true SecondInterceptor preHandle . (First First, after the Second, three configuration [-2])

2, the console output is as follows:

FirstInterceptor ---> preHandle

3. Conclusion: SecondInterceptor and Controller are not executed.

4, FirstInterceptor SecondInterceptor sequence and configuration, but the preHandle FirstInterceptor returns true, SecondInterceptor the preHandle returns to false . (First First, after the Second, three configuration [-2])

FirstInterceptor  ---> preHandle
SecondInterceptor  ---> preHandle
FirstInterceptor  ---> afterCompletion

5. Conclusions: interceptors as long as there are returns false, postHandle method does; in a single interceptor, preHandle returns true, afterCompletion will be performed.

 

V. Summary

1, configured in accordance springmvc.xml interceptor order, law as follows:

  • preHandle order of execution (arranged on the front, first)

  • postHandle performed in reverse order. (Arranged on the front, executing)

  • afterCompletion 同 postHandle 。

 

2, a plurality of interceptors are, as long as it returns false, then the method does postHandle. (All returns true, postHandle method was executed)

3, a plurality of interceptors are, as long as it returns false, then the corresponding Controller is not performed.

4, a single interceptor, preHandle returns true, afterCompletion will be performed.

5, a single interceptor, preHandle returns false, preHandle and afterCompletion not be executed.

 

 

Download Source:  https://gitee.com/RunCoder/spring-mvc-tourist/tree/interceptor

 

Published 156 original articles · won praise 159 · views 490 000 +

Guess you like

Origin blog.csdn.net/HaHa_Sir/article/details/103901616