春天拦截器没有射击

时间:2011-06-22 05:28:31

标签: java spring

我正在尝试按此处http://www.vaannila.com/spring/spring-interceptor.html

所述实施日志记录

这是处理程序映射文件:

<?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 [com.audiClave.controllers.RestController@18f110d] 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.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

也许这个类在列表中定位不正确?

2 个答案:

答案 0 :(得分:1)

不确定这些会有所帮助,但尝试使用

答案 1 :(得分: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>