On the scope of request, session and global session in spring learning

For the scope related to the web container, first configure it in the web container.

Request scope

Consider the following bean definitions:

For each HTTP request, the spring container will create a new loginaction bean instance according to the loginaction bean definition, and the loginaction bean instance is only valid in the current HTTP request. Therefore, you can safely change the internal state of the created instance according to the needs, and the instances created according to the loginaction bean definition in other requests, You will not see these state changes specific to a request. When the processing of the request ends, the bean instance of the request scope will be destroyed.

Session scope

Consider the following bean definitions:

For an HTTP session, the spring container will create a new userpreferences bean instance according to the userpreferences bean definition, and the userpreferences bean is only valid in the current HTTP session. Like the request scope, you can safely change the internal state of the created instance as needed. For instances created according to userpreferences in other HTTP sessions, you will not see these state changes specific to an HTTP session. When the HTTP session is finally discarded, the beans within the scope of the HTTP session will also be discarded.

Global session scope

Consider the following bean definitions:

The global session scope is similar to the standard HTTP session scope, but it only makes sense in portlet based web applications. The portlet specification defines the concept of global session, which is shared by all the different portlets that make up a portlet web application. The beans defined in the global session scope are limited to the life cycle of the global portlet session.

Please note that if you are writing a standard servlet based web application and define one or more beans with global session scope, the system will use the standard HTTP session scope and will not cause any errors.

Scope dependency problem

If you want to inject (for example) an HTTP request scoped bean into another bean of a longer-lived scope,you may choose to inject an AOP proxy in place of the scoped bean. That is,you need to inject a proxy object that exposes the same public interface as the scoped object but that can also retrieve the real target object from the relevant scope (such as an HTTP request) and delegate method calls onto the real object.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>