我正在尝试使用注释驱动的 Controller 配置来注册拦截器。据我所知,我已经正确地完成了所有操作,但是当我尝试测试拦截器时,没有任何 react 。在查看日志后,我发现了以下内容:
2010-04-04 20:06:18,231 DEBUG [main] support.AbstractAutowireCapableBeanFactory (AbstractAutowireCapableBeanFactory.java:452) - Finished creating instance of bean 'org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping#0'
2010-04-04 20:06:18,515 DEBUG [main] handler.AbstractDetectingUrlHandlerMapping (AbstractDetectingUrlHandlerMapping.java:86) - Rejected bean name 'org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping#0': no URL paths identified
2010-04-04 20:06:19,109 DEBUG [main] support.AbstractBeanFactory (AbstractBeanFactory.java:241) - Returning cached instance of singleton bean 'org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping#0'
查看此日志片段的第二行。 Spring是否拒绝DefaultAnnotationHandlerMapping bean?如果是这样,这可能是我的拦截器无法正常工作的问题吗?
这是我的应用程序上下文:
<?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:mvc="http://www.springframework.org/schema/mvc"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"
default-autowire="byName">
<!-- Configures the @Controller programming model -->
<mvc:annotation-driven />
<!-- Scan for annotations... -->
<context:component-scan base-package="
com.splash.web.controller, com.splash.web.service, com.splash.web.authentication"/>
<bean id="authorizedUserInterceptor" class="com.splash.web.handler.AuthorizedUserInterceptor"/>
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="interceptors">
<list>
<ref bean="authorizedUserInterceptor"/>
</list>
</property>
</bean>
这是我的拦截器:
package com.splash.web.handler;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
public class AuthorizedUserInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
log.debug(">>> Operation intercepted...");
return true;
}
}
有人看到这有什么问题吗?我上面提到的错误实际上是什么意思,它对不被调用的拦截器有影响吗?谢谢!
最佳答案
您所看到的是<mvc:annotation-driven />
与DefaultAnnotationHandlerMapping
的显式bean定义之间的冲突。
当您添加<mvc:annotation-driven />
时,Spring会声明自己的DefaultAnnotationHandlerMapping
,并且由于它出现在您自己的DefaultAnnotationHandlerMapping
之前,因此它具有优先权。您的拦截器已使用<mvc:annotation-driven />
注册,但实际上从未被调用。
尝试删除ojit_code,然后重试。