我正在尝试按照此处所述实现日志记录 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
调用 Controller ,但不调用拦截器。 为什么拦截器不会被调用?我使用的是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
也许该类在列表中的位置不正确??
最佳答案
不确定这些是否有帮助,但尝试使用
org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping
而不是DefaultAnnotationHandlerMapping
和替换 schemaLocation 以匹配您的 Spring 版本:
... 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
关于java - Spring 拦截器不发射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6435315/