Spring Bean Scopes
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-
1. Singleton:(Default)
Scopes a single bean definition to a single object instance per Spring IoC container.
2. Prototype:
Scopes a single bean definition to any number of object instances.
But at web environment spring have three more scopes as following:
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.
Popular Tutorials
- Singleton Bean Scope – Return a single bean instance per Spring IoC container
- Prototype Bean Scope – Return a new bean instance each time when requested
1. Singleton Bean Scope:
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.
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.
@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.
1. Request Scope:
<bean class="com.dineshonjava.Point" id="point" scope="request"></bean>
2. Session Scope:
<bean class="com.dineshonjava.Point" id="point" scope="session"></bean>
3. global-session:
<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
- Spring Interview Questions and Answers
- Spring AOP Interview Questions and Answers
- Spring MVC Interview Questions
- Spring Security Interview Questions and Answers
- Spring REST Interview Questions and Answers
- Spring Boot Interview Questions and Answers
- Spring Boot Microservices Interview Questions and Answers
- Dependency Injection (DI) in Spring
- Spring IoC Container
- What is Bean Factory in Spring
- ApplicationContext in Spring
- Bean Autowiring in Spring
- Spring Bean Scopes
- Create Custom Bean Scope in Spring Example
- Using ApplicationContextAware in Spring
- Spring Bean Life Cycle and Callbacks
- BeanPostProcessor in Spring
- BeanFactoryPostProcessor in Spring
- Annotations in Spring and Based Configuration
- Spring JSR-250 Annotations
- JSR 330 Annotations in Spring
- Spring @Component, @Repository, @Service and @Controller Stereotype Annotations
- Method injection with Spring using Lookup method property
- Spring AOP-Introduction to Aspect Oriented Programming
- @Aspect Annotation in Spring
- Spring AOP AspectJ @Before Annotation Advice Example
- Spring AOP Before Advice Example using XML Config
- Spring AOP AspectJ @After Annotation Advice Example
- Spring AOP After Advice Example using XML Config
- Spring AOP AspectJ @AfterReturning Annotation Advice Example
- Spring AOP After-Returning Advice Example using XML Config
- Spring AOP AspectJ @AfterThrowing Annotation Advice Example
- Spring AOP After Throwing Advice Example using XML Config
- Spring AOP AspectJ @Around Annotation Advice Example
- Spring AOP Around Advice Example using XML Config
- Spring AOP Proxies in Spring
- Spring AOP Transaction Management in Hibernate
- Spring Transaction Management
- Spring Declarative Transaction Management Example
- Spring AOP-Ordering of Aspects with Example
- Spring Security Java Based Configuration with Example
- Spring Security XML Namespace Configuration Example
best