<上下文:组件扫描>如果在 XML 中定义处理程序映射,则不会获取我的 @RequestMappings

发布于 2024-12-04 23:35:49 字数 1807 浏览 0 评论 0 原文

我在控制器上使用带有 @RequestMapping 注释的 Spring 3.0.5。这可行,并且 URL 由包扫描注册。

但是,当我在 XML 配置中定义了处理程序映射时,就会出现问题。不再选取 @RequestMapping 注释。

我已将问题隔离到一个简单的应用程序。

如果我有以下控制器:

package test.pack.age;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class TestController {
    @RequestMapping(value="/test")
    public String showTestPage() {
        return "testPage";
    }
}

和以下配置:

<context:component-scan base-package="test.pack.age" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
  <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
  <property name="prefix" value="/WEB-INF/jsp/" />
  <property name="suffix" value=".jsp" />
</bean>

应用程序正常工作并且 URL /test 已注册并正常工作。

18/09/2011  20:02:55    org.springframework.web.servlet.handler.AbstractUrlHandlerMapping   INFO    Mapped URL path [/test] onto handler 'testController'
18/09/2011  20:02:55    org.springframework.web.servlet.handler.AbstractUrlHandlerMapping   INFO    Mapped URL path [/test] onto handler 'testController'

但是,如果我将处理程序映射添加到 XML,它就不再起作用。即使是像这样简单的东西:

<bean id="handlerMappings" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" />

它基本上什么也不做,并且 不再注册我的 URL。

我需要为某些(第三方)控制器提供额外的处理程序映射,但我无法对其进行注释,但是在添加它时,它会破坏我的所有 @RequestMapping

这是正常的吗?一个错误? (我无法更改 Spring 版本)

我错过了什么吗?

I'm using Spring 3.0.5 with <context:component-scan> and @RequestMapping annotations on my controllers. This works, and URLs are registered by the package scan.

But there is a problem when I have a handler mapping defined in the XML config. The @RequestMapping annotations are no longer picked up.

I've isolated the problem to a simple application.

If I have the following controller:

package test.pack.age;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class TestController {
    @RequestMapping(value="/test")
    public String showTestPage() {
        return "testPage";
    }
}

and the following configuration:

<context:component-scan base-package="test.pack.age" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
  <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
  <property name="prefix" value="/WEB-INF/jsp/" />
  <property name="suffix" value=".jsp" />
</bean>

The application works correctly and the URL /test is registered and works properly.

18/09/2011  20:02:55    org.springframework.web.servlet.handler.AbstractUrlHandlerMapping   INFO    Mapped URL path [/test] onto handler 'testController'
18/09/2011  20:02:55    org.springframework.web.servlet.handler.AbstractUrlHandlerMapping   INFO    Mapped URL path [/test] onto handler 'testController'

But if I add a handler mapping to the XML it no longer works. Even something simple like this:

<bean id="handlerMappings" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" />

which basically does nothing, and the <context:component-scan> no longer registers my URL.

I need an extra handler mapping for some (third party) controllers which I can't annotate, but when adding it it breaks all my @RequestMappings.

Is this normal? A bug? (I can't change the Spring version)

Am I missing something?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梨涡 2024-12-11 23:35:49

这正常吗?一个错误? (我无法更改 Spring 版本)

我错过了什么吗?

我错过了一些东西:D。发现这个埋在Spring的JavaDocs中 DefaultAnnotationHandlerMapping

注意:如果您在 DispatcherServlet 上下文中定义自定义 HandlerMapping bean,则需要显式添加 DefaultAnnotationHandlerMapping bean,因为自定义 HandlerMapping bean 会替换默认映射策略。

将其添加到我的配置 XML 中,现在一切正常:

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />

Is this normal? A bug? (I can't change the Spring version)

Am I missing something?

I was missing something :D. Found this buried in the Spring's JavaDocs for DefaultAnnotationHandlerMapping:

NOTE: If you define custom HandlerMapping beans in your DispatcherServlet context, you need to add a DefaultAnnotationHandlerMapping bean explicitly, since custom HandlerMapping beans replace the default mapping strategies.

Added this to my config XML and now everything works:

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
倾其所爱 2024-12-11 23:35:49

我不确定为什么您的带注释的 Controller 不再被拾取,但我认为您的问题在于您提供的 SimpleUrlHandlerMappingid 。它应该是handlerMapping

I am not sure why your annotated Controller is not being picked up anymore, but I think your issue lies in the id you provide your SimpleUrlHandlerMapping. It should be handlerMapping.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文