无法添加HandleInterceptorAdapter的实例

时间:2015-09-23 16:14:22

标签: java spring spring-mvc spring-boot interceptor

我试图拦截进入Spring Boot应用程序的每个HTTP请求。 为此,我有一个LoggingInterceptor如下:

@Component
public class LoggingInterceptor extends HandlerInterceptorAdapter {

private static final Logger logger = LoggerFactory.getLogger(LoggingInterceptor.class);

public LoggingInterceptor() {
    super();
    logger.debug("LoggingInteceptor constructor");
}
@Override
public boolean preHandle(HttpServletRequest request,
        HttpServletResponse response, Object handler) throws Exception {
    logger.debug("LoggingInteceptor: Intercepted  " + request.getRequestURI());
            return true;

}

}

然后,我有一个@Configuration类,用于将拦截器添加到注册表中,如下所示:

@EnableWebMvc
@Configuration
public class LoggingConfig extends WebMvcConfigurerAdapter {
@Autowired
LoggingInterceptor loggingInterceptor;

public void addInterceptors(InterceptorRegistry registry){
    registry.addInterceptor(loggingInterceptor);
}

}

但是,我只是看不到任何记录器语句。为了实现这个目标,我还需要做些什么吗?

1 个答案:

答案 0 :(得分:1)

您的配置看起来很好。如果拦截器为空,registry.addInterceptor(loggingInterceptor);调用也会失败,因此它似乎是正确连接的。

因此我会核实:

  1. 如果在日志框架中启用了DEBUG级别
  2. 如果两个类都包含在Spring上下文中(放置断点addInterceptors方法并启动应用程序)
  3. 修改

    好的,所以评论确认你的问题是我的第一点。 您需要找到日志记录配置文件(例如log4j.properties,log4j,xml或logback.xml)并将日志级别从INFO更改为DEBUG。