The properties of the bean in spring scope

Source: Internet
Author: User

The scope property of the bean in spring has the following 5 types:

    1. Singleton represents a singleton in a spring container that always returns a unique instance when the bean is obtained through the spring container
    2. Prototype means a new object is generated each time a bean is obtained
    3. Request indicates valid for one HTTP request (web App only)
    4. Session is valid for a user session (web app only)
    5. Globalsession means valid within a global session (Web app only)

In most cases, we will only use Singleton and prototype two scopes, if the scope attribute is not specified within the spring configuration file, the default is singleton.

Below we use an example to illustrate the difference between the Singleton and prototype two scopes.

Add the contents of the Java class Person,person as follows:

 Packagecn.outofmemory.spring; Public classPerson {Private intID; PrivateString name;  Public intgetId () {returnID; }     Public voidSetId (intID) { This. ID =ID; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }}

The person class is a Pojo class, and we don't need him to do anything just to prove the similarities and differences between the two scopes of prototype and Singleton.

We also need an app class whose code is as follows:

 Packagecn.outofmemory.spring;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;/*** Hello world! **/ Public classApp { Public Static voidMain (string[] args) {ApplicationContext appContext=NewClasspathxmlapplicationcontext ("/spring.xml"); Person P1= Appcontext.getbean (person.class); System.out.println ("P1 ' s Identityhashcode" +System.identityhashcode (p1)); Person P2= Appcontext.getbean (person.class); System.out.println ("P2 ' s Identityhashcode" +System.identityhashcode (p2)); }}

In the main method of the App class, we first initialize the instance of ApplicationContext, then get the instance of the person bean two times p1,p2 and output its identityhashcode separately, if two objects are the same object, Then their identityhashcode should be the same.

Add source folder:src/main/conf, and create a new spring profile spring.xml:

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:context= "Http://www.springframework.org/schema/context"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/context/spring-cont Ext-3.0.xsd ">        <Beanclass= "Cn.outofmemory.spring.Person">        < Propertyname= "id"value= "1"/>        < Propertyname= "Name"value= "Jim"/>    </Bean>    </Beans>

In this configuration file we define the person bean, does not specify its scope, this time the bean's scope is the default singleton, that is, a singleton, the output of the app class should be two identical identityhashcode, We run the program to see the results of the output:

P1' s identityhashcode is 23954271p2 'is23954271  


    <Beanclass= "Cn.outofmemory.spring.Person"Scope= "Prototype">        < Propertyname= "id"value= "1"/>        < Propertyname= "Name"value= "Jim"/>    </Bean>    

Run the program again and the output is as follows:

P1' s identityhashcode is 23954271p2 'is13359324  

You can see that P1 and P2 are two different values, which means that the same bean definition will return different objects if scope is prototype.

We can also specify the scope of the Java bean through the scope annotations, and we add the following annotations to the person class:

ImportOrg.Springframework.Contextannotation. Scope; import Org.. Stereotype. Component;  @Component  @Scope  () public  person { .... Omit }             

@Component note tells Spring that to load this class, scope of the scope annotation bean is prototype.

We modify the Spring.xml configuration file, use the Context:component-scan node, and let spring load the bean through annotations.

<?XML version="1.0"Encoding="UTF-8"?><beans xmlns="Http://www.springframework.org/schema/beans"Xmlns:xsi="Http://www.w3.org/2001/XMLSchema-instance" xmlns:context=       xsi:schemalocation= "http://www.springframework.org/schema/beans           http:// www.springframework.org/schema/beans/spring-beans.xsd           http:// www.springframework.org/schema/context           http://www.springframework.org/ Schema/context/spring-context-3.0.xsd "> <context: Component-scan base-package=></context:component-scan> </BEANS>             

Run the program again, and the output will be:

P1' s identityhashcode is 33212498p2 'is24480977  

You can see that two person objects are two different objects after you specify prototype scope using the scope annotations.

Original address: Http://outofmemory.cn/java/spring/spring-bean-scope

The properties of the bean in spring 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.