在根应用程序上下文中声明的 Spring @Controllers
Spring Web 应用程序有两个上下文 - Web 上下文(子上下文)和根上下文(父上下文)。如果 @Controller
bean 是在 dispatcher-servlet.xml
(Web 上下文)中定义的,那么一切都很好。
但是,如果控制器 bean 是在父上下文 (applicationContext.xml
) 中定义的,则控制器将无法识别 - 即它们的映射未配置,因此当您尝试打开 /foo/ 时bar
在这样的控制器中定义,找不到路径。
问题是:无论 bean 定义在哪里,如何使控制器映射能够被解析。
PS 我知道我可以将声明移动到子上下文,但我不想这样做,原因超出了这个问题的范围。
Spring web applications have two context - the web context (child) and the root context (parent). If @Controller
beans are defined in dispatcher-servlet.xml
(the web context) everything is fine.
But if the controller beans are defined in the parent context (applicationContext.xml
), then the controllers are not recognized - i.e. their mappings are not configured, so when you try to open /foo/bar
defined in such controller, the path is not found.
The question is: how to make the controller mappings to be parsed no matter where the beans are defined.
P.S. I know I can move the declarations to the child context, but I don't want to do that for reasons beyond the scope of this question.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
DefaultAnnotationHandlerMapping
有一个detectHandlersInAncestorContexts
属性启用所需的行为。DefaultAnnotationHandlerMapping
has adetectHandlersInAncestorContexts
property that enables the desired behaviour.