2011-06-22 69 views
0

我試圖在此描述http://www.vaannila.com/spring/spring-interceptor.htmlSpring攔截不點火

這是處理程序映射文件來實現記錄:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:p="http://www.springframework.org/schema/p" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:oxm="http://www.springframework.org/schema/oxm" 
xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-3.0.xsd 
     http://www.springframework.org/schema/oxm 
     http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd"> 

    <bean id="beanNameResolver" 
     class="org.springframework.web.servlet.view.BeanNameViewResolver"/> 

    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" 
    p:interceptors-ref="loggerInterceptor" /> 
    <context:component-scan base-package="com.audiClave.controllers" /> 
    <bean id="loggerInterceptor" class="com.audiClave.controllers.LoggerInterceptor" /> 

</beans> 

這裏是攔截器:

package com.audiClave.controllers; 

... 

public class LoggerInterceptor extends HandlerInterceptorAdapter { 

static Logger logger = Logger.getLogger(LoggerInterceptor.class); 

static{ 
    BasicConfigurator.configure(); 
      logger.setLevel((Level)Level.INFO); 
} 

@Override 
public boolean preHandle(HttpServletRequest request, 
     HttpServletResponse response, Object handler) throws Exception { 
    logger.info("Before handling the request"); 
    return super.preHandle(request, response, handler); 
} 

@Override 
public void postHandle(HttpServletRequest request, 
     HttpServletResponse response, Object handler, 
     ModelAndView modelAndView) throws Exception { 
    logger.info("After handling the request"); 
    super.postHandle(request, response, handler, modelAndView); 
} 

@Override 
public void afterCompletion(HttpServletRequest request, 
     HttpServletResponse response, Object handler, Exception ex) 
     throws Exception { 
    logger.info("After rendering the view"); 
    super.afterCompletion(request, response, handler, ex); 
} 
} 

以下消息出現在控制檯中:

Mapping [/REST/en/actions] to HandlerExecutionChain with handler [[email protected]] and 3 interceptors 

控制器被調用,但不是攔截器。 爲什麼攔截器不會被調用?我正在使用Spring 3.0.5

我曾嘗試在所有事件中放置一個調試斷點並且沒有被觸發。已將日誌記錄設置爲INFO,但仍然沒有輸出。

的loggerInterceptor被拾起,因爲下面的日誌聲明:

2011-06-22 21:11:39,828 [ContainerBackgroundProcessor[StandardEngine[Catalina]]] INFO org.springframework.beans.factory.support.DefaultListableBeanFactory - Destroying singletons in org.s[email protected]f2ea42: defining beans [beanNameResolver,org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping#0,baseController,restController,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,loggerInterceptor]; root of factory hierarchy 

也許類列表中的位置不正確?

+0

嘗試使用下面的鏈接提供的例子看起來與你相似:HTTP://www.vaannila.com/spring/spring-interceptors.html –

+0

你確定記錄器的INFO級別已啓用? (一個幾乎相同的代碼 - 它的工作) –

+0

謝謝,但我調整了文件是類似的,攔截器仍然沒有調用。 –

回答

0

以下工作:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:p="http://www.springframework.org/schema/p" 
xmlns:mvc="http://www.springframework.org/schema/mvc" 
xmlns:context="http://www.springframework.org/schema/context" 
xsi:schemaLocation=" 
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 

<bean id="loggerInterceptor" class="com.audiClave.controllers.LoggerInterceptor" /> 
<bean id="handlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" 
    p:interceptors-ref="loggerInterceptor" /> 
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --> 

<!-- Scans within the base package of the application for @Components to configure as beans --> 
<!-- @Controller, @Service, @Configuration, etc. --> 
<context:component-scan base-package="com.audiClave.controllers" /> 

</beans> 
1

不知道這會幫助,但嘗試使用

+0

嗨,感謝您的意見,試過了您的建議,但沒有運氣。 –