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, you may only deal with the Spring’s core scope – singleton and prototype, and the default scope is singleton.
Singleton vs Prototype
Here’s an example to show you what’s the different between bean scope : singleton andprototype.packagecom
.mkyong
.customer
.services
;
publicclassCustomerService
{
String message
;
public
String
getMessage(){
return
message
;
}
publicvoidsetMessage(
String message
){
this.
message
=message
;
}
}
1. Singleton example
If no bean scope is specified in bean 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/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="customerService"
class="com.mkyong.customer.services.CustomerService" />
</beans>
Run itpackagecom
.mkyong
.common
;
importorg
.springframework
.context
.ApplicationContext
;
importorg
.springframework
.context
.support
.ClassPathXmlApplicationContext
;
importcom
.mkyong
.customer
.services
.CustomerService
;
publicclassApp
{
publicstaticvoidmain(String
[]args
)
{
ApplicationContext context
=
newClassPathXmlApplicationContext(newString[]{"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());
}
}
OutputMessage
:Message by custA
Since the bean ‘customerService’ is in singleton scope, the second retrieval by ‘custB’ will display the message set by ‘custA’ also, even it’s retrieve by a new getBean() method. In singleton, only a single instance per Spring IoC container, no matter how many time you retrieve it with getBean(), it will always return the same instance.Message
:Message by custA
2. Prototype example
If you want a new ‘customerService’ bean instance, every time you call 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/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="customerService" class="com.mkyong.customer.services.CustomerService"
scope="prototype"/>
</beans>
Run it againMessage
:Message by custA
In prototype scope, you will have a new instance for eachMessage
:null
getBean()
method called.Request scope vs Prototype scope
Prototype creates a brand new instance everytime you call getBean on the ApplicationContext. Whereas for Request, only one instance is created for an HttpRequest. So in a single HttpRequest, I can call getBean twice on Application and there will ever be one bean instantiated, whereas that same bean scoped to Prototype in that same single HttpRequest would get 2 different instances.
HttpRequest scope
Mark mark1 = context.getBean("mark");
Mark mark2 = context.getBean("mark");
mark1 == mark2; //This will return true
Prototype scope
Mark mark1 = context.getBean("mark");
Mark mark2 = context.getBean("mark");
mark1 == mark2; //This will return false
No comments:
Post a Comment