Spring Bean Scope

Source: Internet
Author: User
Tags getmessage

Spring Bean Scope


In spring, bean scope was used to decide which type of bean instance should was return from Spring container back to the CAL Ler.

5 Types of Bean scopes supported:

    1. Singleton–return A single bean instance per Spring IoC container

    2. Prototype–return A new Bean instance each time when requested

    3. Request–return A single bean instance per HTTP request. *

    4. Session–return A single bean instance per HTTP session. *

    5. Globalsession–return A single bean instance per global HTTP session. *

In most cases, the deal with the Spring's core Scope–singleton and prototype, and the default scope is Singleto N.


P.S * means only valid in the context of a web-aware Spring ApplicationContext


Singleton vs Prototype

Here's a example to show you the ' s the different between Bean Scope:singleton and prototype.

Package Usoft;public class CustomerService {String message;    Public String GetMessage () {return message;    The public void Setmessage (String message) {this.message = message; }}


1. Singleton Example

If No bean scope is specified in beans configuration file, default to Singleton.

<beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance "xsi:schemalocation=" http://www.springframework.org/schema/beanshttp:// Www.springframework.org/schema/beans/spring-beans-2.5.xsd "> <bean id=" customerservice "class=" com . Mkyong.customer.services.CustomerService "/> </beans>

Run it

package com.mkyong.common; import org.springframework.context.applicationcontext;import  Org.springframework.context.support.classpathxmlapplicationcontext; package usoft;public class  app {    public static void main (String[] args)  {         ApplicationContext context =                 new  Classpathxmlapplicationcontext (new string[]{"Spring-customer.xml"});         CustomerService custA =  (CustomerService)  context.getbean ("CustomerService");         custa.setmessage ("Message by custa");         system.out.println ("message : "  + custa.getmessage ());         //retrieve it again        customerservice custb =   (CustomerService)  context.getbean ("CustomerService");         system.out.println ("message : "  + custb.getmessage ());     }}

Output

Message:message by Custa

Message:message by Custa

Since The bean ' customerservice ' is in singleton scope, and the second retrieval by ' CUSTB ' would display the message set by ' C Usta ' Also, even it ' s retrieve by a new Getbean () method. In Singleton, instance per Spring IoC container, no matter what many time you retrieve it with Getbean (), it Would always return the same instance.


2. Prototype Example

If you want a new ' customerservice ' bean instance, every time for it, use prototype instead.

<beans xmlns= "Http://www.springframework.org/schema/beans" xmlns:xsi= "http://www.w3.org/2001/ Xmlschema-instance "xsi:schemalocation=" http://www.springframework.org/schema/beanshttp:// Www.springframework.org/schema/beans/spring-beans-2.5.xsd "> <bean id=" customerservice "class=" Com.mkyong.customer.services.CustomerService "scope=" prototype "/> </beans>

Run it again

Message:message by Custa

Message:null

In the prototype scope, you'll have a new instance for each Getbean () method called.


3. Bean Scopes Annotation

You can also with annotation to define your bean scope.

Package Usoft;import Org.springframework.context.annotation.scope;import org.springframework.stereotype.service;@    Service@scope ("prototype") public class CustomerService {String message;    Public String GetMessage () {return message;    The public void Setmessage (String message) {this.message = message; }}

Enable Auto Component Scanning

<beans xmlns= "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/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp:// Www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd " > <context:component-scan base-package= "Com.mkyong.customer"/> </beans>


Spring Bean 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.