On scope scope scope in spring

Today, I studied the scope of scope. The default is singleton mode, that is, scope = "Singleton". In addition, the scope also has the scope of prototype, request, session and global session. Scope = "prototype" multiple cases. When reconfiguring the scope of a bean, its header file form is as follows: how to use the scope of spring:

The scope here is used to configure the scope of the spring bean, which identifies the scope of the bean.

In spring 2 Before 0, beans had only two scopes: singleton (singleton) non-singleton (also known as prototype), after spring 2.0, three kinds of beans dedicated to web application context, session, request and global session, have been added. Therefore, by default, spring 2.0 now has five types of beans. Of course, spring 2.0 refactored the design of bean types and designed flexible bean type support. Theoretically, there can be countless types of beans and users You can add new bean types according to your needs to meet the needs of practical applications.

1. Singleton scope

When the scope of a bean is set to singleton, there will only be one shared bean instance in the spring IOC container, and all requests for a bean will only return the same instance of the bean as long as the ID matches the bean definition. In other words, when a bean definition is set to singleton scope, the spring IOC container will only create a unique instance of the bean definition. This single instance is stored in the single instance cache (singleton cache), and all subsequent requests and references to the bean will return cached object instances. Note that singleton scope is completely different from singleton in GoF design pattern. Singleton design pattern means that only one class exists in a classloader, and singleton here means that a container corresponds to a bean, that is to say When a bean is identified as a singleton, only one bean will exist in the IOC container of spring.

Configuration instance:

2、prototype

For beans deployed in the prototype scope, each request (injecting it into another bean or calling the container's getBean () method programmatically) will generate a new bean instance, which is equivalent to a new operation. For beans in the prototype scope, one thing is very important, that is, spring cannot be responsible for the whole life cycle of a prototype bean, After initializing, configuring, decorating or assembling a prototype instance, the container gives it to the client, and then ignores the prototype instance. Regardless of the scope, the container will call the initialization lifecycle callback methods of all objects, while for prototype, any configured destructor lifecycle callback methods will not be called. It is the responsibility of the client code to clear the objects in the prototype scope and release the expensive resources held by any prototype bean. (one possible way for the spring container to free up resources occupied by singleton scoped beans is by using the bean's post processor, which holds a reference to the bean to be cleared.)

Configuration instance:

3、request

Request means that a new bean will be generated for each HTTP request, and the bean is only valid in the current HTTP request. Configuration instance:

When using request, session and global session, you should first initialize the web XML is configured as follows:

If you are using servlet2 4 and above, then you only need the XML declaration file web Add the following contextlistener to XML:

If it is servlet2 4, then you need to use a javax servlet. Implementation of filter:

Then you can configure the scope of the bean:

4、session

The session scope indicates that a new bean will be generated for each HTTP request, and the bean is only valid in the current HTTP session. Configuration instance:

Configuration instance:

As the premise of request configuration instance, after configuring the web startup file, you can configure it as follows:

5、globalsession

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 portletsession. If you use the global session scope to identify beans in the web, the web will automatically be used as a session type.

Configuration instance:

As the premise of request configuration instance, after configuring the web startup file, you can configure it as follows:

summary

The above is all about the scope scope in spring. I hope it will be helpful to you. Interested friends can continue to refer to other related topics on this site. If there are deficiencies, please leave a message to point out. Thank you for your support!

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
分享
二维码
< <上一篇
下一篇>>