公司使用方式:

Spring 定義:

<bean id="logInterceptor" class="com.XXXeb.cgp.gw.common.interceptor.LogInterceptor" />
<bean
      class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
   <property name="beanNames">
   <list>
        <value>*Controller</value>    // 所有命名為  XXXXController 的class檔
    </list>
</property>
<property name="interceptorNames">
   <list>
       <value>logInterceptor</value>            //  進入com.XXXeb.cgp.gw.common.interceptor.LogInterceptor
   </list>
</property>
</bean>

 

 

  參考網址:  

http://www.technicalkeeda.com/details/spring-interceptor-example  

下面為範例網址copy下來:

<?xml version="1.0" encoding="UTF-8"?><web-appversion="2.4"xmlns="http://java.sun.com/xml/ns/j2ee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
         http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"><servlet><servlet-name>springtutorial</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>springtutorial</servlet-name><url-pattern>*.htm</url-pattern></servlet-mapping><welcome-file-list><welcome-file>
      index.jsp
    </welcome-file></welcome-file-list></web-app>

Spring Interceptor configuration (springtutorial-servlet.xml)

Here we configure two interceptors LoggerInterceptor and PerformanceInterceptor. Spring AOP provides the facility to control execution of interceptors with out modifying your existing code. E.g. In PerformanceInterceptor you can find out the method execution time base on your configuration , here we configure only for controller. For MethodInterceptor you need to define the proxyCreator.

<?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><beanid="helloController"class="com.net.technicalkeeda.controller.HelloController"/><beanid="loggerInterceptor"class="com.net.technicalkeeda.interceptor.LoggerInterceptor"/><beanid="performanceInterceptor"class="com.net.technicalkeeda.interceptor.PerformanceInterceptor"/><beanid="urlMapping"class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"><propertyname="mappings"><props><propkey="hello.htm">helloController</prop></props></property><propertyname="interceptors"><list><refbean="loggerInterceptor"/></list></property></bean><beanname="proxyCreator"class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"><propertyname="beanNames"><list><value>*Controller</value></list></property><propertyname="interceptorNames"><list><value>performanceInterceptor</value></list></property></bean></beans>

Interceptor classes

LoggerInterceptor.java extends HandlerInterceptorAdapter , in that we need to override the preHandle(), postHandle() and afterCompletion() methods.

1] preHandle() method calls before the handler execution, returns a Boolean value "true" and "false".

2]postHandle() method calls after the handler execution, Here you can manipulate the ModelAndView object before render it to view page.

3]afterCompletion() method calls after the complete request has finished.

package com.net.technicalkeeda.interceptor;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.web.servlet.ModelAndView;import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;publicclassLoggerInterceptorextendsHandlerInterceptorAdapter{@Overridepublicboolean preHandle(HttpServletRequest request,HttpServletResponse response,Object handler)throwsException{System.out.println("Calling preHandle");returnsuper.preHandle(request, response, handler);}@Overridepublicvoid postHandle(HttpServletRequest request,HttpServletResponse response,Object handler,ModelAndView modelAndView)throwsException{System.out.println("Calling postHandle");super.postHandle(request, response, handler, modelAndView);}@Overridepublicvoid afterCompletion(HttpServletRequest request,HttpServletResponse response,Object handler,Exception ex)throwsException{System.out.println("Calling afterCompletion");super.afterCompletion(request, response, handler, ex);}}

PerformanceInterceptor.java class

Performance Interceptor implements the MethodInterceptor. Here you need to implement the invoke() method. It gives MethodInvocation object to retrieve the method details. In below example we find out the method execution time

package com.net.technicalkeeda.interceptor;import org.aopalliance.intercept.MethodInterceptor;import org.aopalliance.intercept.MethodInvocation;publicclassPerformanceInterceptorimplementsMethodInterceptor{publicObject invoke(MethodInvocation method)throwsThrowable{long startTime =System.currentTimeMillis();try{Object result = method.proceed();return result;}finally{long endTime =System.currentTimeMillis();long executionTime = endTime - startTime;System.out.println("Method Name: "+ method.getMethod().getName()+" Execution Time:- "+ executionTime +" ms");}}}

Finish and Output

When you access the HelloController using URL "http://localhost:8080/SpringTutorial/hello.htm" it display below output , of both Interceptors on server console.

Calling preHandle
Calling HelloController.handleRequest()
Method Name: handleRequest Execution Time:- 1 ms
Calling postHandle
Calling afterCompletion

 

 

 

 

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 JoshS 的頭像
    JoshS

    JoshS的部落格

    JoshS 發表在 痞客邦 留言(0) 人氣()