繁体   English   中英

spring controller 组件的默认 scope 是什么?

[英]What is the default scope for a spring controller component?

我正在深入研究 spring bean 范围和应用程序上下文主题,并尝试理解一些概念。

我知道有三个主要背景。 ApplicationContext(这是独立应用程序的上下文)、基于 web 的应用程序的 WebApplicationContext(扩展 ApplicationContext)和 ServletContext(与 Spring 无关,而是 JEE 事物,也与 Z2567A5EC9705EB7AC2C9848 应用程序有关)。

一个 WebApplicationContext 包含所有 Web 相关的 bean(控制器、ViewResolver 等....),扩展一个 ApplicationContext 并引用一个 ServletContext

an ApplicationContext contains beans with singleton and prototype scope and a WebApplicationContext adds three scopes: request, session, websocket and application but application scoped beans are related to the ServletContext referenced in the WebApplicationContext.

我不确定的是:

  • 如果 controller 组件包含在 WebApplicationContext 中,那么它们的默认 scope 是什么? 我以为是 singleton 但在这种情况下它没有任何意义,因为包含这些 bean 的 WebApplicationContext 在服务器关闭时会死掉

  • the documentation says that application scope is "somewhat similar to a Spring singleton bean but differs in two important ways: It is a singleton per ServletContext, not per Spring ApplicationContext (for which there may be several in any given web application ), and it is实际暴露并因此作为 ServletContext 属性可见。” 它很清楚,但我不明白我们如何为一个 applicationContext 拥有多个 servletContext,我没有找到任何用例。 I've seen a usecase for multiple WebApplicationContext in the same app, like an app whith a REST API and a web front, but in this case, there is only one applicationContext and (I thought) only one servletContext shared by multiple WebApplicationContext.

I don't think there is a conflict between that Controller scope being a Singleton and the WebApplicationContext that contains those beans dies when the server is shut down, for the controller scope being a Singleton this means that the FactoryBeans creates one and only one istance of这个 controller 并在调用时使用它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM