Bean scope in spring

Source: Internet
Author: User

When defining beans in the configuration file, you can not only configure bean attribute values and dependencies between them, but also define bean scopes.

The scope will affect the bean lifecycle and creation method. In earlier versions of spring, there are only two scopes: singleton and prototype.

In sprign2.0, three new scopes are added for webapplicationcontext: request, session, and globalsession.

 

1. Singleton and prototype

In earlier versions of spring, because there are only two scopes: singleton and prototype, Singleton = "True | false" is used. Spring still supports this configuration method for backward compatibility, however, spring recommends a new configuration method: Scope = "Scope type ".

1. Singleton Scope

Spring provides the natural single-instance mode function in the form of containers. Any pojo can use the single-instance mode without writing special code.

Spring uses the functions of AOP and localthread to specially process non-thread-safe variables, so that these non-thread-safe classes become thread-safe classes.

<Bean id = "car1" class = "Spring. IOC. demo1.car "Scope =" Singleton "P: Brand =" Spring injection-Hongqi 001 "P: color =" Spring injection-purple "P: maxspeed =" 520 "/>
Public static void main (string [] ARGs) {applicationcontext CTX = new classpathxmlapplicationcontext ("classpath: applicationcontext. XML "); // bean car1 of Singleton = CTX. getbean ("car1", car. class); system. out. println ("car1:" + car1); car1.setbrand ("changed"); car car2 = CTX. getbean ("car1", car. class); system. out. println ("obtain car1 of the Singleton again:" + car2 );
}

Output:

Car1: The car is: Spring injection-Hongqi 001, color is: Spring injection-purple, maxspeed is: 520
Obtain car1: The car is: changed, color is: Spring injection-purple, maxspeed is: 520

By default, when the spring applicationcontext container is started, all Singleton beans are automatically instantiated and buffered in the container.

A. In this way, some potential configuration problems will be discovered as soon as possible by instantiating the bean in advance.

B. the bean will be saved as a cache and will no longer be instantiated when used again

If you do not want to instantiate the singleton bean when the container is started, you can control the bean by using the lazy-init attribute:

<Bean id = "car1" class = "Spring. IOC. demo1.car "Scope =" Singleton "P: Brand =" Spring injection-Hongqi 001 "P: color =" Spring injection-purple "P: maxspeed = "520" lazy-init = "true"/>

The bean of lazy-init = "true" will still be instantiated in advance in some special cases: if the bean is referenced by other beans that need to be instantiated in advance.

 

2. Prototype Scope

By default, the spring container does not instantiate the prototype bean at startup. In addition, after the spring container delivers the prototype bean to the caller, it no longer manages its lifecycle.

<Bean id = "carprototype" class = "Spring. IOC. demo1.car "Scope =" prototype "P: Brand =" Bond "P: color =" black "P: maxspeed =" 110 "/>
Public static void main (string [] ARGs) {applicationcontext CTX = new classpathxmlapplicationcontext ("classpath: applicationcontext. XML "); // prototype bean car carprototype1 = CTX. getbean ("carprototype", car. class); system. out. println ("carprototype:" + carprototype1); carprototype1.setbrand ("changed"); car carprototype2 = CTX. getbean ("carprototype", car. class); system. out. println ("obtain the carprototype of multiple instances again:" + carprototype2 );}

Output:

Carprototype: The car is: bond, color is: Black, maxspeed is: 110
Obtain the carprototype: The car is: bond, color is: Black, maxspeed is: 110

Every time you call getbean, you will get a bean in the latest state.

 

II. Scope of bean related to the web application environment

1. Request Scope

<Bean id = "car" class = "Spring. IOC. demo1.car "Scope =" request "P: Brand =" Bond "P: color =" black "P: maxspeed =" 110 "/>

The bean in the request scope corresponds to an HTTP request and lifecycle. Each time an HTTP request arrives at carbean, the spring container creates a new carbean. After the request is processed, the bean is destroyed.

2. Session Scope

<Bean id = "car" class = "Spring. IOC. demo1.car "Scope =" session "P: Brand =" Bond "P: color =" black "P: maxspeed =" 110 "/>

The scope of carbean will span the entire http session, and all HTTP requests in the session will share a carbean. After the http session ends, the instance will be destroyed.

 

3. globalsession Scope

<Bean id = "car" class = "Spring. IOC. demo1.car "Scope =" globalsession "P: Brand =" Bond "P: color =" black "P: maxspeed =" 110 "/> in

The globalsession scope is similar to the session scope, but it is only used in the Web applications of the Portlet. The Portlet Specification defines the global sesson concept, which is shared by all the sub-Portlet that compose the portletweb application. If it is not used in a web application of the Portlet, globalsession is equivalent to the session scope.

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.