Spring拒绝bean名称,没有指定URL路径

时间:2010-04-05 04:33:24

标签: java spring spring-mvc

我正在尝试使用注释驱动的控制器配置注册拦截器。据我所知,我已经做了一切正确但当我尝试测试拦截器时没有任何反应。查看日志后,我发现了以下内容:

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

有人看到这个有什么问题吗?我上面提到的错误实际上是什么意思,它是否可以对拦截器没有被调用?谢谢!

2 个答案:

答案 0 :(得分:17)

您所看到的是<mvc:annotation-driven />DefaultAnnotationHandlerMapping的显式bean定义之间的冲突。

当您添加<mvc:annotation-driven />时,Spring会声明自己的DefaultAnnotationHandlerMapping,并且因为它出现在您自己的DefaultAnnotationHandlerMapping之前,它会获得优先权。您的拦截器已在<mvc:annotation-driven />注册,但实际上从未被调用过。

尝试删除{{1}}并重试。

答案 1 :(得分:5)

http://forum.springsource.org/showthread.php?t=81238找到。使用

<mvc:annotation-driven/>

接着是

<mvc:interceptors> <bean class="com.splash.web.handler.AuthorizedUserInterceptor"/></mvc:interceptors>