Spring Bean Scopes
Spring Bean Scope
In the spring
bean configurations, bean attribute called 'scope' defines what kind of object
has to created and returned. There are 5 types of bean scopes available, they
are:
1) singleton: Returns a single bean instance per
Spring IoC container.
2) prototype: Returns a new bean instance each time
when requested.
3) request: Returns a single instance for every HTTP
request call.
4) session: Returns a single instance for every HTTP
session.
5) global session: global session scope is equal as session
scope on portlet-based web applications.
If no bean scope is specified
in bean configuration file, then it will be by default 'singleton'.
The scope of a bean defines the life cycle
and visibility of that bean in the contexts in which it is used.
Spring defines 5 types of scopes:
- singleton
- prototype
- request
- session
- globalSession
When defining a <bean> in Spring, you have the option of
declaring a scope for that bean. For example, To force Spring to produce a new
bean instance each time one is needed, you should declare the bean's scope
attribute to be prototype.
Similar way if you want Spring to return the same bean instance each time one
is needed, you should declare the bean's scope attribute to be singleton.
The Spring
Framework supports following five scopes, three of which are available only if
you use a web-aware ApplicationContext.
Scope
|
Description
|
singleton
|
This scopes the bean definition to a single instance per
Spring IoC container (default).
|
prototype
|
This scopes a single bean definition to have any number of
object instances.
|
request
|
This scopes a bean definition to an HTTP request. Only valid
in the context of a web-aware Spring ApplicationContext.
|
session
|
This scopes a bean definition to an HTTP session. Only valid
in the context of a web-aware Spring ApplicationContext.
|
global-session
|
This scopes a bean definition to a global HTTP session. Only
valid in the context of a web-aware Spring ApplicationContext.
|
Syntax:
<bean id="…"
class="…" scope="…">
<!-- collaborators and configuration for
this bean go here -->
</bean>
The singleton scope:
The default scope is always singleton.
If scope is set to singleton, the Spring IoC container creates
exactly one instance of the object defined by that bean definition. This single
instance is stored in a cache of such singleton beans, and all subsequent
requests and references for that named bean return the cached object.
Defining a bean with singleton scope means the container creates a single instance of
that bean, and all requests for that bean name will return the same object,
which is cached. Any modifications to the object will be reflected in all
references to the bean. This scope is the default value if no other scope is
specified.
The prototype scope:
If scope is
set to prototype, the Spring IoC container creates new bean instance of the
object every time a request for that specific bean is made. As a rule, use the prototype
scope for all state-full beans and the singleton scope for stateless beans.
A bean with prototype scope will
return a different instance every time it is requested from the container.
request/session/globalSessoin:
The request scope
creates a bean instance for each HTTP request. Session scope
creates an instance for an HTTP Session, and the globalSession scope
creates an instance for a global HTTP Session.
Practical Example:
Car.java
package com.smgc;
public class Car {
private String
color;
public String
getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
Beans.xml
<?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="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="car_sin" class="com.smgc.Car">
<property name="color"
value="Blue"></property>
</bean>
<bean id="car_pro" class="com.smgc.Car"
scope="prototype"></bean>
</beans>
TestCarMain.java
package
com.smgc;
import
org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
public class
TestCarMain {
public static void main(String[]
args) {
// TODO Auto-generated
method stub
ApplicationContext ctx =
new ClassPathXmlApplicationContext("Beans.xml");
Car c1 = (Car)
ctx.getBean("car_sin");
System.out.println("c1
= " + c1.getColor());
Car c2 = (Car)
ctx.getBean("car_sin");
System.out.println("c2
= " + c2.getColor());
c2.setColor("Green");
System.out.println("c1
= " + c1.getColor());
System.out.println("c2
= " + c2.getColor());
Car c3 = (Car)
ctx.getBean("car_pro");
c3.setColor("Red");
System.out.println("c3
= " + c3.getColor());
Car c4 = (Car)
ctx.getBean("car_pro");
c4.setColor("Yello");
System.out.println("c4
= " + c4.getColor());
}
}
Output:
c1 = Blue
c2 = Blue
c1 = Green
c2 = Green
c3 = Red
c4 = Yello
Comments
Post a Comment