Java Spring 拒绝 bean 名称,未指定 URL 路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2576939/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring rejecting bean name, no URL paths specified
提问by richever
I am trying to register an interceptor using a annotation-driven controller configuration. As far as I can tell, I've done everything correctly but when I try testing the interceptor nothing happens. After looking in the logs I found the following:
我正在尝试使用注释驱动的控制器配置注册拦截器。据我所知,我已经正确完成了所有操作,但是当我尝试测试拦截器时没有任何反应。查看日志后,我发现以下内容:
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'
Look at the second line of this log snippet. Is Spring rejecting the DefaultAnnotationHandlerMapping bean? And if so could this be the problem with my interceptor not working?
查看此日志片段的第二行。Spring 是否拒绝 DefaultAnnotationHandlerMapping bean?如果是这样,这可能是我的拦截器无法正常工作的问题吗?
Here is my application context:
这是我的应用程序上下文:
<?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>
Here is my interceptor:
这是我的拦截器:
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;
}
}
Does anyone see anything wrong with this? What does the error I mentioned above actually mean and could it have any bearing on the interceptor not being called? Thanks!
有没有人看到这有什么问题?我上面提到的错误实际上是什么意思,它是否与未被调用的拦截器有任何关系?谢谢!
采纳答案by skaffman
What you're seeing is a clash between <mvc:annotation-driven />
and the explicit bean definition of DefaultAnnotationHandlerMapping
.
您所看到的是<mvc:annotation-driven />
和 的显式 bean 定义之间的冲突DefaultAnnotationHandlerMapping
。
When you add <mvc:annotation-driven />
, Spring declares its own DefaultAnnotationHandlerMapping
, and because it appears before your own one, it gets priority. Your interceptor is registered with your DefaultAnnotationHandlerMapping
, but that never actually get called.
当您添加 时<mvc:annotation-driven />
,Spring 会声明它自己的DefaultAnnotationHandlerMapping
,并且因为它出现在您自己的 之前,所以它获得优先权。您的拦截器已注册到您的DefaultAnnotationHandlerMapping
,但实际上从未被调用。
Try removing <mvc:annotation-driven />
and trying again.
尝试删除并重<mvc:annotation-driven />
试。
回答by dileeph
found from http://forum.springsource.org/showthread.php?t=81238. Use
从http://forum.springsource.org/showthread.php?t=81238找到。用
<mvc:annotation-driven/>
followed by
其次是
<mvc:interceptors> <bean class="com.splash.web.handler.AuthorizedUserInterceptor"/></mvc:interceptors>