简体   繁体   English

Spring 是什么意思 3、web-aware application context

[英]What does it mean in Spring 3, web-aware application context

I am trying to setup a session scoped bean but the spring document said that session scope is only applicable on web-aware application context.我正在尝试设置一个 session 作用域 bean,但 spring 文档说 session scope 仅适用于网络感知应用程序上下文。 No further explanation in the doc.文档中没有进一步的解释。 Can someone clarify this?有人可以澄清一下吗?

This means that you can only use the session scoped beans in a an application deployed to a web server.这意味着您只能在部署到 Web 服务器的应用程序中使用会话范围的 bean。 Spring can be used in applications that run in standard JVMs along with applications that run in servlet containers (Tomcat, etc). Spring 可用于在标准 JVM 中运行的应用程序以及在 servlet 容器(Tomcat 等)中运行的应用程序。 Session, however, only exists in web servers so it has no meaning if the application is running in a standard desktop environment.但是,会话仅存在于 Web 服务器中,因此如果应用程序在标准桌面环境中运行,则它没有任何意义。

There are basically 5 types of scopes available for spring bean. Spring bean 基本上有 5 种类型的作用域。 1)Singleton 2)Prototype 3)Request 4)Session 5)Global-Session 1)Singleton 2)Prototype 3)Request 4)Session 5)Global-Session

The first two scopes can be used with any type of spring applications.前两个范围可用于任何类型的弹簧应用程序。 But the remaining 3 are related to web applications.但其余 3 个与 Web 应用程序相关。 They can be used only with the spring applications which are involved in web.它们只能与 web 中涉及的 spring 应用程序一起使用。

Web aware means when the application provides web endpoints for third party client. Web 感知意味着应用程序为第三方客户端提供 Web 端点。 IE When the application contains at least one RestController . IE 当应用程序包含至少一个RestController 时 You can do this by simply adding @RestController annotation to your class.您可以通过简单地将@RestController注释添加到您的类中来做到这一点。

ApplicationContext is an interface, spring ships multiple ApplicationContext implementations, according to documentation you need to use one that is web-aware. ApplicationContext 是一个接口,spring 提供了多个 ApplicationContext 实现,根据您需要使用的文档,您需要使用一个具有 web 感知能力的实现。

The request, session, application, and websocket scopes are available only if you use a web-aware Spring ApplicationContext implementation (such as XmlWebApplicationContext).请求、session、应用程序和 websocket 范围仅在您使用 Web 感知 Spring ApplicationContext 实现(例如 XmlWebApplicationContext)时可用。 If you use these scopes with regular Spring IoC containers, such as the ClassPathXmlApplicationContext, an IllegalStateException that complains about an unknown bean scope is thrown.如果您将这些作用域与常规 Spring IoC 容器(例如 ClassPathXmlApplicationContext)一起使用,则会抛出 IllegalStateException,抱怨未知的 bean scope。

And as of spring framework core (6.0.4) further configuration might be required :从 spring 框架核心 (6.0.4) 开始,可能需要进一步配置

To support the scoping of beans at the request, session, application, and websocket levels (web-scoped beans), some minor initial configuration is required before you define your beans.为了在请求、session、应用程序和 websocket 级别(Web 范围的 beans)支持 beans 的范围,在定义 beans 之前需要一些小的初始配置。

... ...

If you access scoped beans within Spring Web MVC, in effect, within a request that is processed by the Spring DispatcherServlet, no special setup is necessary.如果您在 Spring Web MVC 中访问作用域 bean,实际上,在由 Spring DispatcherServlet 处理的请求中,不需要特殊设置。 DispatcherServlet already exposes all relevant state. DispatcherServlet 已经公开了所有相关的 state。

If you use a Servlet web container, with requests processed outside of Spring's DispatcherServlet (for example, when using JSF), you need to register the org.springframework.web.context.request.RequestContextListener ServletRequestListener.如果您使用 Servlet web 容器,请求在 Spring 的 DispatcherServlet 之外处理(例如,当使用 JSF 时),您需要注册 org.springframework.web.context.request.RequestContextListener ServletRequestListener。 This can be done programmatically by using the WebApplicationInitializer interface.这可以通过使用 WebApplicationInitializer 接口以编程方式完成。 Alternatively, add the following declaration to your web application's web.xml file:或者,将以下声明添加到您的 web 应用程序的 web.xml 文件中:

<web-app>
    ...
    <listener>
        <listener-class>
            org.springframework.web.context.request.RequestContextListener
        </listener-class>
    </listener>
    ...
</web-app>

Spring boot will automatically configure this for you (couldn't find documentation mentioning this explicitly). Spring boot 会自动为你配置这个(找不到明确提到这个的文档)。

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

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