开发者

Spring Interceptors not firing

I am attempting to implement logging as described here http://www.vaannila.com/spring/spring-interceptor.html

This is the handler mapping file:

<?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:ox开发者_运维知识库m="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>

Here is the interceptor:

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);
}
}

The following message appears in the console:

Mapping [/REST/en/actions] to HandlerExecutionChain with handler [com.audiClave.controllers.RestController@18f110d] and 3 interceptors

The controller is called, but not the interceptor. Why wouldn't the interceptors be called? I am using Spring 3.0.5

I have tried putting a debug breakpoint in all of the events and none are fired. Have set the logging to INFO but still no output.

The loggerInterceptor is being picked up because of the following log statement:

2011-06-22 21:11:39,828 [ContainerBackgroundProcessor[StandardEngine[Catalina]]] INFO  org.springframework.beans.factory.support.DefaultListableBeanFactory - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@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

Maybe the class is positioned incorrectly in the list??


Not sure these will help but try to use

  • org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping instead of DefaultAnnotationHandlerMapping and
  • replace the schemaLocation to match your Spring version:

    ... 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


The following worked:

<?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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜