Skip to content
  • Facebook
GeekCer Logo

GeekCer

The geek's Coding education and Review centre

  • Home
  • Tutorials
    • Java
    • Servlet
    • JSP
    • Python
    • C Tutorial
    • Spring
    • Spring Boot
    • MongoDB
    • Hibernate
    • Data Structure
  • General Knowledge
  • Biography
  • Grammar
  • Festival (त्योहार)
  • Interview
  • Differences
  • Important
  • Toggle search form

Home » Spring » Bean Scopes in Spring, Types of Bean Scopes, Examples

  • Jhansi Ki Rani Lakshmi Bai History, Story, Information in Hindi
    Jhansi Ki Rani Lakshmi Bai History, Story, Information in Hindi Biography
  • International Labour's Day in Hindi | अंतर्राष्ट्रीय मजदूर दिवस 1 मई
    International Labour’s Day in Hindi | अंतर्राष्ट्रीय मजदूर दिवस 1 मई General Knowledge
  • Apj Abdul Kalam biography in Hindi, Life, Missile Man of India
    Apj Abdul Kalam biography in Hindi, Life, Missile Man of India Biography
  • Indian Navy Day
    Indian Navy Day: जल सेना दिवस कब और क्यों मनाया जाता है? General Knowledge
  • Famous Slogans given by famous Personalities Indian leaders
    Famous Slogans of freedom fighters in Hindi | स्वतंत्रता सेनानियों के प्रसिद्ध नारे हिंदी में General Knowledge
  • Kishkindha Kand in Hindi | Ram meets Hanuman | किष्किंधा कांड
    Kishkindha Kand in Hindi | Ram meets Hanuman | किष्किंधा कांड Spiritual
  • Lata Mangeshkar Biography In Hindi | लता मंगेशकर संपूर्ण जीवन परिचय
    Lata Mangeshkar Biography In Hindi | लता मंगेशकर संपूर्ण जीवन परिचय Biography
  • World Earth Day in Hindi | पृथ्वी दिवस कब और क्यों मनाया जाता है?
    Earth Day in Hindi, Theme | पृथ्वी दिवस कब और क्यों मनाया जाता है? General Knowledge

Bean Scopes in Spring, Types of Bean Scopes, Examples

Posted on January 30, 2022January 30, 2022 By GeekCer Education No Comments on Bean Scopes in Spring, Types of Bean Scopes, Examples
Bean Scopes in Spring, Types of Bean Scopes, Examples

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
    • Singleton Scope in Spring
    • Prototype Scope in Spring
    • Request Scope in Spring
    • Session Scope in Spring
    • Application Scope of Spring
    • Global-session Scope in Spring
  • Example of Singleton scope and Prototype scope in Spring
    • Example of Singleton Scope
    • Example of Prototype Scope

Types of Bean Scopes in Spring

As per our utility and requirement, we are provided with the following 6 types of scopes

  1. Singleton
  2. Prototype
  3. Request
  4. Session
  5. Application
  6. 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

Share this:

  • Click to share on Facebook (Opens in new window)
  • Click to share on WhatsApp (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • More
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Pinterest (Opens in new window)

Also Read

Spring, Spring Boot Tags:Default scope of bean in spring, How bean scope is defined in Spring boot?, Spring bean scope annotation, What are the scopes in Spring and explain?

Post navigation

Previous Post: Top Spring Boot Annotations
Next Post: HTTP status codes List | Response Status Code Glossary

More Related Articles

Spring Initializr for Spring Boot Project Spring Initializr for Spring Boot Project Spring Boot
Spring Boot Tutorial Spring Boot Tutorial Spring Boot
Top Spring Boot Annotations Top Spring Boot Annotations Spring Boot
Mockito unit testing spring boot Mockito Framework Tutorial for Unit testing | Mockito unit testing spring boot Important
HTTP status codes List | Response Status Code Glossary HTTP status codes List | Response Status Code Glossary Important

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • National Farmers Day in Hindi | राष्ट्रीय किसान दिवस पर निबंध | चौधरी चरण सिंह जयंती
  • Human rights day in Hindi: 10 दिसंबर ह्यूमन राइट्स डे
  • Unicef day is celebrated on December 11 | Speech on unicef day
  • Indian Navy Day: जल सेना दिवस कब और क्यों मनाया जाता है?
  • P V Sindhu Biography in Hindi, Badminton, State, Caste पी. वी. सिंधु जीवन परिचय, कहानी, राज्य, जाति
  • Draupadi Murmu Biography In Hindi | द्रौपदी मुर्मू की जीवनी
  • OSI Model | 7 Layers of OSI Model in Computer network
    OSI Model | 7 Layers of OSI Model in Computer network, Functions Networking
  • Network kya hai (नेटवर्क क्या है)
    Network kya hai (नेटवर्क क्या है) Networking
  • Difference between TCP and UDP
    Difference between TCP and UDP | TCP vs UDP examples Differences
  • Similarities and difference between OSI and TCP/IP model
    OSI vs TCP/IP Model, Similarities and difference between OSI and TCP/IP model Networking
  • Difference between Internet and Intranet
    Difference between Internet and Intranet Differences
  • TCP/IP Model, Full Form, Layers and their Functions
    TCP/IP Model, Full Form, Layers and their Functions Networking
  • IPv4 Vs IPv6 | Difference between IPv4 and IPv6
    IPv4 Vs IPv6 | Difference between IPv4 and IPv6 Differences
  • Java Tutorial
  • Servlet Tutorial
  • JSP Tutorial
  • Maven Tutorial
  • HTML Tutorial
  • Programs
  • Hindi/English Grammar
  • Difference Between ... and ...
  • HR Interview
  • Important Articles

Write to Us:
geekcer.code@gmail.com

  • About Us
  • Privacy and Policy
  • Disclaimer
  • Contact Us
  • Sitemap

Copyright © GeekCer 2022 All Rights reserved