In Spring we get the bean from the
Spring container with some default configuration.
Default behavior is that every beans in the Spring container are initialized when bean configuration file loaded to the JVM. Whenever
getBean is called container recognized the
bean by given bean id and return that bean to the caller.
One more default behavior is that every beans has only one instance in the spring container.
Bean Scope means which is used to decide which type of bean instance should be return from
Spring container back to the caller. Basic Spring Bean Scopes are only two types-
Singleton:(Default) Scopes a single bean definition to a single object instance per Spring IoC container.
Prototype: Scopes a single bean definition to any number of object instances. But at web environment spring have three more scopes
Request Scope: This scopes a bean definition to an HTTP request.
Session Scope: This scopes a bean definition to an HTTP session.
Global Session: Scopes a single bean definition to the lifecycle of a global HTTP Session.
We can control not only the various
dependencies and configuration values that are to be plugged into an object that is created from a particular bean definition, but also the
scope of the objects created from a particular bean definition. This approach is very powerful and gives you the flexibility to
choose the scope of the objects you create through configuration.
Basic Spring Bean Scope are only two types are given below.
- Singleton Bean Scope - Return a single bean instance per Spring IoC container
- Prototype Bean Scope - Return a new bean instance each time when requested
When a bean is a
singleton, only one
shared instance of the bean will be managed, and all requests for beans with an
id or id
s matching that bean definition will result in that one specific bean instance being returned by the
Spring container.
We can say another way,
when you define a bean definition and it is scoped as a singleton, then the
Spring IoC container will create
exactly one instance of the object defined by that bean definition. This single instance will be stored in a cache of such singleton beans, and
all subsequent requests and references for that named bean will result in the
cached object being returned.
To define a singleton scope, you can set the
scope property to
singleton in the bean configuration file, as shown below:
<bean class="com.dineshonjava.sdnext.beanscope.Point" id="zeroPoint" scope="singleton">
<property name="x" value="0"></property>
<property name="y" value="0"></property>
</bean>
See the singleton scope of bean with full example.
Using Annotation for Bean Scope:
@Service
@Scope("singleton")
public class Point
{
private int x;
private int y;
public void setX(int x){
this.x = x;
}
public void setY(int y){
this.y = y;
}
}
See the singleton scope of bean with full example.
NOTE : This singleton is differ from the singleton pattern in Java Class.
Single pattern in java mean you can create the only one instance of a that class in JVM.
But In spring singleton bean scope means every container can create only single bean in the Spring IoC Container but a JVM can have multiple Spring IoC Container so JVM can multiple beans rather than bean singleton bean scope.
2. Prototype Bean 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.
To define a prototype scope, you can set the
scope property to
prototype in the bean configuration file, as shown below:
<bean class="com.dineshonjava.sdnext.beanscope.Point" id="zeroPoint" scope="prototype"><property name="x" value="0"></property>
<property name="y" value="0"></property>
</bean>
See the full example of prototype bean scope with using the Annotation.
Using Annotation for Bean Scope:
@Service
@Scope("prototype")
public class Point
{
private int x;
private int y;
public void setX(int x){
this.x = x;
}
public void setY(int y){
this.y = y;
}
}
See the full example of prototype bean scope with using the Annotation.