
Bean scopes play an essential role in the life cycle of the bean. It specifies the bean’s scope. In other words, it establishes the bean’s visibility.
If you want a bean to be produced every time you use it, or if you don’t want a bean to be formed every time you use the same bean object, for example. Spring bean scope is used to decide such scenarios.
Table of Contents
Types of Bean Scopes in Spring
As per our utility and requirement, we are provided with the following 6 types of scopes
- Singleton
- Prototype
- Request
- Session
- Application
- global-session
XML based configuration example
<bean id="beanId" class="com.geekcer" scope="<scope-type>" />
Annotation based configuration example
@Component @Scope("singleton") public class <bean-class-name> { } // Similarly you can use @Scope("prototype"), @Scope("request"), @Scope("session"), @Scope("application")
Singleton Scope in Spring
The bean’s default scope is the singleton scope. We declare scope singleton, which instructs the container to create a single intent for the bean. For all bean names, only one object will be returned. If we change the object, the change will be reflected in all references to the bean.
Prototype Scope in Spring
The container will create a new bean instance for each request if you provide a prototype scope for a bean.
When to use singleton and prototype scope in spring? If you wish to specify bean scope, prototype scope for stateless beans and singleton scope for stateless beans are the best options as a rule of thumb.
Request Scope in Spring
In web applications, the bean’s request scope is used. A new instance of the bean is created when an HTTP request is made.
Session Scope in Spring
If you provide the bean session scope, it indicates that just one instance of the bean will be available during the lifespan of the HTTP session. It is used for the User level session.
Application Scope of Spring
In the case of application scope, a single instance will be created and made available throughout the ServletContext’s lifecycle.
Global-session Scope in Spring
You should use the global-session scope if you wish to construct session beans for a portlet application. If you have a portal and wish to keep certain variables for global access, you can make a global session in this scenario. It is used for the Application level session.
Note: Only in the context of a web-aware Spring ApplicationContext are request, session, and global-session scopes valid.
You should first register a RequestContextListener or RequestContextFilter before using the Request, Session, Application, or WebSocket scopes.
Example of Singleton scope and Prototype scope in Spring
Player.java
package com.geekcer; public class Player { private String name; public void setName(String name) { this.name = name; } public void getName() { System.out.println("Player name is " + name); } }
MainApplication.java
package com.geekcer; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainApp { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml"); Player object1 = (Player) context.getBean("pname"); object1.setName("Virat Kohli"); object1.getName(); Player object2 = (Player) context.getBean("pname"); object2.getName(); } }
Example of Singleton Scope
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" xsi:schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id = "pname" class = "com.geekcer.Player" scope = "singleton"> </bean> </beans>
When you run MainApplication.java class you will get output like this.
Output: Player name is Virat Kohli Player name is Virat Kohli
Example of Prototype Scope
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" xsi:schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id = "pname" class = "com.geekcer.Player" scope = "prototype"> </bean> </beans>
If you run MainApplication.java class after converting scope to prototype you will get output like this. One thing to note here is that the name in the second line is null.
Output: Player name is Virat Kohli Player name is null
Click here to learn the spring boot