Spring Bean Scopes 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-
Scopes a single bean definition to a single object instance per Spring IoC container.
Scopes a single bean definition to any number of object instances.
But at web environment spring have three more scopes as following:
This scopes a bean definition to an HTTP request.
This scopes a bean definition to an HTTP 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.
Popular Tutorials
Scopes a single bean definition to a single object instance per Spring IoC container. This is the default behavior of the spring container.
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 ids 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.
@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.
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.
@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.
<bean class="com.dineshonjava.Point" id="point" scope="request"></bean>
<bean class="com.dineshonjava.Point" id="point" scope="session"></bean>
<bean class="com.dineshonjava.Point" id="point" scope="globalSession"></bean>
Please note that if you are writing a standard Servlet-based web application and you define one or more beans as having global session scope, the standard HTTP Session scope will be used, and no error will be raised.
Spring Related Topics you may like
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…