SpringMVC interceptor (d)

Interceptors, similar to the nature of AOP, the main scenarios:

  • Logging: log requests information, information for monitoring, statistical information, and the like PV is calculated.
  • Permission checks: detection, such as login, enter the processor detects whether a login, login not return to the login page.
  • Performance Monitoring: recording time interceptors into the processor and out of the processor.
  • General Behavior: reading user information in the cookie into the request so as to facilitate the subsequent use of the process, as well as Locale, Theme extract information, as long as it requires a plurality of processors may be implemented using interceptors.
  • OpenSessionView: As Hibernate, the processor enters the opening in the Session, Session is closed after completion.

Interceptor implemented in two ways:

1. To achieve interceptor processor interface: org.springframework.web.servlet.HandlerInterceptor

  • preHandle (HttpServletRequest request, HttpServletResponse response, Object handler): pre-callback method, before performing Controller, returns true proceed to the next process (interceptor or handler). Return false break execution, will not call interceptor or processor can be operated reponse to generate the response.
  • postHandle (HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView): callback method after treatment, after Controller (front view rendering) executed by ModeAndView can be treated or processed to view, may be null ModeAndView
  • afterCompletion (HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex): the entire request completed callback method, when the view is rendered and callbacks. 

2. A successor interceptor adapter class: org.springframework.web.servlet.handler.HandlerInterceptorAdapter

  Achieve three interfaces interceptor needs to be rewritten, the interceptor adapter do for these three methods to achieve the air can be inherited this class, a method of rewriting to 3 interceptor needed.

the difference:

  • Interceptor adapter made three empty implemented method interceptor interface, which may require 1-3 replication method.

  • AsyncHandlerInterceptor interceptor implements the interface adapter providing afterConcurrentHandlingStarted () function, the Controller for processing asynchronous requests

Project Configuration:

<!-- 拦截器定义 -->
<bean id="logInterceptor" class="com.wang.interceptor.LogInterceptor"/>
<bean id="performanceInterceptor" class="com.wang.interceptor.PerformanceInterceptor"/>
<bean id="testInterceptorAdapter" class="com.wang.interceptor.TestInterceptorAdapter"/>

<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
  <!-- 拦截器配置,顺序执行 -->
  <property name="interceptors">
    <list>
                 <ref bean="logInterceptor"/>
         <ref bean="performanceInterceptor"/>
         <ref bean="testInterceptorAdapter"/>
      </list>
    </property>
</bean>

 

Guess you like

Origin www.cnblogs.com/myitnews/p/11568856.html