Scope of Spring---bean

Source: Internet
Author: User

Concept

The scope described in the Spring Bean, which is the scope property in the configuration file. In object-oriented programming, generally refers to the visible range between an object or a variable. In the spring container, it refers to the requested visible range of the bean object it creates relative to the other bean objects

Type of scope

In the spring container, there are 5 scope types provided, in the configuration file, the scope range of the bean is set through the property scope.

    • Singleton:<bean id= "UserInfo" class= "Com.jike.UserInfo" scope= "Singleton" ></bean>

There will only be one shared bean instance in the Spring IOC container, and all requests to the bean will only return the same instance of the bean as long as the ID matches the bean. That is, when a bean's scope property is set to Singleton, the Spring IOC container only creates a unique instance of the bean definition, and the singleton is stored in a singleton cache. And all subsequent requests and references to the bean will return an instance of the object that was cached. Singleton is the default value.

Singleton scope refers to the existence of only one bean instance in the spring IOC container, the bean exists as a single instance, and the single instance mode is one of the important design patterns, in the traditional application development, we need to write the specific code for each single instance class manually. In this case, the business logic code of the class is tightly coupled with the pattern code, and spring provides a natural single-instance mode function as a container. There is no need to write special code, only through configuration, you can implement single-instance mode. In general, a stateless or state-immutable class is suitable for use with single-instance mode. Spring, however, transcends this by using a single-instance pattern for those objects that are not thread-safe.

By default, spring's ApplicationContext container automatically instantiates all the singleton beans and caches them in the container at startup. Although it takes some time to start, it brings two benefits.

1. Early instantiation of the bean can lead to early detection of some potential configuration errors;

2.Bean is saved as a cache, and when used to the bean, it is no longer instantiated, which speeds up the related operational efficiency. If the user does not need to instantiate the Bean Singleton in advance of the container startup, it can be controlled by setting the Lazy-init property to True, or it can be controlled, if a bean is also used singleton scope, and the bean is referenced. will also be instantiated in advance.

    • Prototype:<bean id= "UserInfo" class= "Com.jike.UserInfo" scope= "prototype" ></bean>

Causes the Spring IOC container to create an instance of a new bean every time a request is made to the bean. For stateful beans, you should use prototype, while stateless beans should use singleton

A prototype scope is a new instance that is returned each time a bean is called from a container, which is the equivalent of executing the new bean () every time the Getbean () is called. By default, the spring container does not instantiate the prototype bean at startup

    • Request:<bean id= "UserInfo" class= "Com.jike.UserInfo" scope= "Request" ></bean>

For each HTTP request, the spring container creates a completely new bean instance based on the associated bean definition, and the instance is valid only within the request of the current HTTP, so you can safely change the internal state of the created instance as needed, and in other requests, Depending on the instance created by the bean definition, you will not see a state transformation for a request for those specific domains.

    • Session:<bean id= "UserInfo" class= "Com.jike.UserInfo" scope= "Session" ></bean>

Works for an HTTP session. The spring container creates a completely new instance of the bean based on the Bean's definition, and the bean is valid only within the current HTTP session. As with the request scope, you can safely change the internal state of the created instance as needed, and other HTTP sessions will not see the state change of these specific domain HTTP sessions based on the instances created by the bean. When the HTTP session is eventually discarded, the bean instances created within the HTTP session scope are discarded.

    • Global Session:<bean id= "UserInfo" class= "Com.jike.UserInfo" scope= "Globalsession" ></bean>

Similar to the standard HTTP session scope, but only in external applications is meaningful.

Scope of Spring---bean

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.