In the previous
chapter ( Spring Bean Definition ), we have seen how we can declare the class
and create its objects using beans.xml.
As we declare a
scope for a variable similarly, a spring bean can also have a scope. In the Beans.xml
we use <bean> attribute to define the bean. We can declare the scope of the
bean also using the scope attribute.
For example, we
can declare the bean scope as Prototype if we want spring to create the
bean instance every time it is needed.
Spring framework
provides five scopes, out of which three are available only if we are using web-aware
ApplicationContext. The scopes are as follows:
1.
Singleton –
with this scope,
the bean instance will be created only once. This is the default scope of
spring bean.
2.
Prototype –
with this scope,
the bean instance will be created each time the object is created.
3.
Request –
with this scope,
the bean instance will be available within the HTTP request. Valid in the
context of a web-aware Spring ApplicationContext.
4.
Session –
with this scope
the bean instance will be available withing HTTP session. Valid in the context
of a web-aware Spring ApplicationContext.
5.
Global-session -
with this scope
the bean instance will be available withing global HTTP session. Valid in the
context of a web-aware Spring ApplicationContext.
In this chapter, Singleton
and Prototype scope will be discussed. Request, session, and
global-session will be discussed with web-aware Spring ApplicationContext.
Singleton scope
If for a spring
bean, the scope is set to singleton, the Spring IoC container will create the
object of the bean only once. The created object will be stored inside the
cache and whenever the object is created after that the reference of the old object will be returned.
Singleton is the
default scope. To define a bean scope as a singleton, declare as shown below:
<!------------- bean
definition with singleton
scope--------------------------------------!> |
For the working
example, we will create three files. HelloWorld.java, MainApp.java and Beans.xml
Below are the
contents of these files:
HelloWorld.java
MainApp.java
Beans.xml
After creating the
source and configuration file, run the application, the output will show that
although we have created two objects the second object will refer to the first
instance only.
Output
Your message :
Object A
Your message : Object A
Prototype scope
If for a bean, the scope is set to prototype, the Spring IoC container will create the instance of
the object every time the request is made.
As a good practice,
we should use the prototype for all state-full beans and the singleton
scope for stateless beans.
To define a bean
scope as a prototype, declare as shown below:
<!------------- bean definition with singleton
scope--------------------------------------!> |
For the working
example, we will create three files. HelloWorld.java, MainApp.java and Beans.xml
Below are the
contents of these files:
HelloWorld.java
MainApp.java
Beans.xml
After creating the
source and configuration file, run the application, the output will show that
although we have created two objects the second object will refer to the first
instance only.
Output
Your message :
Object A
Your message :
Object B
With this, we come to the end of this chapter, as a next topic please refer “Spring Bean Life Cycle”.
0 Comments