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:
Singleton–return A single bean instance per Spring IoC container
Prototype–return A new Bean instance each time when requested
Request–return A single bean instance per HTTP request. *
Session–return A single bean instance per HTTP session. *
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