在根应用程序上下文中声明的 Spring @Controllers

发布于 2024-12-08 19:45:12 字数 387 浏览 0 评论 0原文

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 技术交流群。

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

发布评论

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

评论(1

海拔太高太耀眼 2024-12-15 19:45:12

DefaultAnnotationHandlerMapping 有一个 detectHandlersInAncestorContexts 属性启用所需的行为。

DefaultAnnotationHandlerMapping has a detectHandlersInAncestorContexts property that enables the desired behaviour.

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