Explore Courses
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Birla Institute of Management Technology Birla Institute of Management Technology Post Graduate Diploma in Management (BIMTECH)
  • 24 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Popular
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science & AI (Executive)
  • 12 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
University of MarylandIIIT BangalorePost Graduate Certificate in Data Science & AI (Executive)
  • 8-8.5 Months
upGradupGradData Science Bootcamp with AI
  • 6 months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
OP Jindal Global UniversityOP Jindal Global UniversityMaster of Design in User Experience Design
  • 12 Months
Popular
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Rushford, GenevaRushford Business SchoolDBA Doctorate in Technology (Computer Science)
  • 36 Months
IIIT BangaloreIIIT BangaloreCloud Computing and DevOps Program (Executive)
  • 8 Months
New
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Popular
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
Golden Gate University Golden Gate University Doctor of Business Administration in Digital Leadership
  • 36 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
Popular
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
Bestseller
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
IIIT BangaloreIIIT BangalorePost Graduate Certificate in Machine Learning & Deep Learning (Executive)
  • 8 Months
Bestseller
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in AI and Emerging Technologies (Blended Learning Program)
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
ESGCI, ParisESGCI, ParisDoctorate of Business Administration (DBA) from ESGCI, Paris
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration From Golden Gate University, San Francisco
  • 36 Months
Rushford Business SchoolRushford Business SchoolDoctor of Business Administration from Rushford Business School, Switzerland)
  • 36 Months
Edgewood CollegeEdgewood CollegeDoctorate of Business Administration from Edgewood College
  • 24 Months
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with Concentration in Generative AI
  • 36 Months
Golden Gate University Golden Gate University DBA in Digital Leadership from Golden Gate University, San Francisco
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Deakin Business School and Institute of Management Technology, GhaziabadDeakin Business School and IMT, GhaziabadMBA (Master of Business Administration)
  • 12 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science (Executive)
  • 12 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityO.P.Jindal Global University
  • 12 Months
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (AI/ML)
  • 36 Months
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDBA Specialisation in AI & ML
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
New
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGrad KnowledgeHutupGrad KnowledgeHutAzure Administrator Certification (AZ-104)
  • 24 Hours
KnowledgeHut upGradKnowledgeHut upGradAWS Cloud Practioner Essentials Certification
  • 1 Week
KnowledgeHut upGradKnowledgeHut upGradAzure Data Engineering Training (DP-203)
  • 1 Week
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
Loyola Institute of Business Administration (LIBA)Loyola Institute of Business Administration (LIBA)Executive PG Programme in Human Resource Management
  • 11 Months
Popular
Goa Institute of ManagementGoa Institute of ManagementExecutive PG Program in Healthcare Management
  • 11 Months
IMT GhaziabadIMT GhaziabadAdvanced General Management Program
  • 11 Months
Golden Gate UniversityGolden Gate UniversityProfessional Certificate in Global Business Management
  • 6-8 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
IU, GermanyIU, GermanyMaster of Business Administration (90 ECTS)
  • 18 Months
Bestseller
IU, GermanyIU, GermanyMaster in International Management (120 ECTS)
  • 24 Months
Popular
IU, GermanyIU, GermanyB.Sc. Computer Science (180 ECTS)
  • 36 Months
Clark UniversityClark UniversityMaster of Business Administration
  • 23 Months
New
Golden Gate UniversityGolden Gate UniversityMaster of Business Administration
  • 20 Months
Clark University, USClark University, USMS in Project Management
  • 20 Months
New
Edgewood CollegeEdgewood CollegeMaster of Business Administration
  • 23 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
KnowledgeHut upGradKnowledgeHut upGradBackend Development Bootcamp
  • Self-Paced
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 5 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
upGradupGradUI/UX Bootcamp
  • 3 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
upGradupGradDigital Marketing Accelerator Program
  • 05 Months

Spring Bean Scopes: Singleton & Prototype Scopes

Updated on 03 July, 2023

6.49K+ views
7 min read

Spring framework is one of the best open-source frameworks for the top-rated programming platform Java and provides massive infrastructure support for easily and quickly developing applications. It was written by Rod Johnson and was first released under the Apache 2.0 license in June 2003.

Application of Spring framework includes its benefits of modular framework, Integration with existing frameworks, testability on fake or development data, well-designed web MVC framework, Central exception handling, Unbelievably lightweight, and onboard transaction management. 

For any object-oriented programming language, one needs to design objects that form your application development’s backbone, and spring manages it using the spring IOC container called beans. A bean is an object managed entirely by the IOC container provided by spring. 

Check out our free courses to get an edge over the competition

The definition of the bean contains three essential factors which come under configuration metadata and includes:

  1. How to create a bean.
  2. The details of the bean’s life cycle.
  3. All the dependencies.

Scopes can be declared while defining a bean and is usually set using scope attribute to one of the five types possible. These five types of scope attribute include the following types:

  1. Singleton: The scope is used to define the scope definition to a single instance every Spring IOC container. This is the default definition of any scope.
  2. Prototype: In some cases, if you want to produce a new bean instance every time needed, it is better to declare the bean’s scope attribute as a prototype.
  3. Request: If you are working on a web-aware spring application, the request scopes the bean definition to an HTTP request.
  4. Session: If you are working on a web-aware spring application context, the session scopes the bean definition to an HTTP session.
  5. Global Session: Global Session scopes a bean definition to a global HTTP session and is only proven in a web-aware Spring Application.

Read: Top Spring Boot Features

Initial Web Configuration:

Configuring bean scopes in Spring application can be done through XML or annotations. The Spring Bean Scopes attribute in XML or the @Scope annotation can be used to define the scope of a bean.

The Singleton Scope

The scope is used to define the scope definition to a single instance of every IOC container in the Spring. This is the default definition of any scope. Now, that single instance is created and pushed in a cache of these singleton beans, and then that cached object can be returned using regular requests. 

As we know, the default declaration for the scope is set to singleton. Still, this code sample can be used to set the scope to the singleton.

Check out upGrad’s Advanced Certification in Cloud Computing 

Enrol in Online Software Development Courses from the World’s top Universities. Earn Executive PG Programs, Advanced Certificate Programs, or Masters Programs to fast-track your career.

Singleton Beans with Prototype-Bean Dependencies:

Handling Singleton beans that have dependencies on Prototype beans can be challenging. By default, the Singleton bean will receive the same instance of the Prototype bean for each request, which may lead to unintended consequences. To manage this scenario, a scoped proxy can be used to ensure that the Singleton bean always receives a new instance of the Prototype bean.

The Prototype Scope

Now, if you are dealing with multiple instance requests for the object every time the user requests that specific bean, in this case, the Spring IoC container creates a new bean instance for the object every time a new request is made. One can use the prototype scope for all state-full beans.

upGrad’s Exclusive Software Development Webinar for you –

SAAS Business – What is So Different?

Request Scope: Code for the request scope:

<bean id=”loginAction” class=”com.foo.LoginAction” scope=”request”/>

Using the loginAction bean definition, the spring container can create a completely new and specific instance for the LoginAction bean for every HTTP request. The change in the specific instance’s main interior state would not affect other instances because these changes created by that same loginAction bean definition would register no changes in the state and are specific to that individual request.

Check out upGrad’s Advanced Certification in Cyber Security

Session scope:

Code for Session Scope:

<bean id=”userPreferences” class=”com.foo.UserPreferences” scope=”session”/>

Using the session scope, the spring container can create a very new UserPreferences instance of a bean using the user preferences bean definition for a complete HTTP session. The change in the specific instance’s main internal state would not affect other instances because these changes created from the same user preferences bean definition would register no changes in the state and are specific to a specific HTTP session. 

Global Session Scope:

Code for the Global Session Scope:

<bean id=”userPreferences” class=”com.foo.UserPreferences” scope=”session”/>

It is mostly similar to the original session scope and only applies to a limited range of portlet based website applications. All these beans, which are defined at the global session scope, are scoped to the end of life for the global portlet Session.

Also Read: Spring Boot Topics & Projects

Scoped Beans as Dependencies:

Spring allows the injection of scoped beans as dependencies in other beans. This enables the utilization of appropriate spring scopes for each dependency, ensuring correct behaviour and lifecycle management.

Application Scope:

Beans with the Application scope exist for the entire lifespan of the application. A single instance of the bean is shared across all users. This scope is suitable for maintaining global application settings and shared resources.

WebSocket Scope:

Introduced in Spring 4.2, the WebSocket scope provides a dedicated bean instance for each WebSocket session. This allows each WebSocket session to have its instance of the bean.

Custom Scopes:

In addition to the built-in scopes, Spring provides the flexibility to create custom scopes tailored to specific requirements. A custom scope involves implementing the Scope interface and defining scoped beans’ instantiation and destruction behaviour.

Creating Your Own Custom Scope:

Once a custom scope is implemented, it can be registered with the Spring application context. This allows using the custom scope in bean definitions using the @Scope annotation or the scope attribute in XML.

Conclusion

Hence, the scope can be declared in various types and disciplines with a limited singleton scope to a global session scope. This helps you to access web applications and is an integral part of the spring bean framework.

Companies are looking for new and innovative technologies to work on, and MongoDB is one of them. These new-age skills are very useful to thrive in a competitive environment such as software development, where priorities change every time a new technology is evolved. 

We at upgrad are here to help you achieve that potential and develop your skills into assets for future companies and organizations by providing real-time inputs and extensive placement support. Make your future secure with us, and don’t let these challenges deprive you of your dreams.

If you’re interested to learn more about Spring Boot, full-stack software development, check out upGrad & IIIT-B’s Executive PG Program in Full-stack Software Development which is designed for working professionals and offers 500+ hours of rigorous training, 9+ projects, and assignments, IIIT-B Alumni status, practical hands-on capstone projects & job assistance with top firms.

Frequently Asked Questions (FAQs)

1. What makes Java so popular today?

Java is extremely popular among developers and programmers for several reasons. The best thing about Java is its platform-independent design. Java programs can be executed on different operating systems as long as the JRE, i.e., Java Runtime Environment, is installed on the system. Even though it has been around for almost 26 years now, Java keeps evolving with time and changing demands of the software industry. Besides, this programming language is considered more straightforward than many other programming languages, making it hugely popular with beginners. It can be used to develop different kinds of apps, starting from full-fledged software packages to mobile apps and web-based applications.

2. Why is Spring named Spring?

Spring is the most widely employed framework for application development in Java enterprise edition (J2EE). It is a powerful but lightweight framework that offers features that can be used to develop any Java application. The name Spring was given to this framework to indicate a fresh start after the so-called winter of conventional J2EE. It was initially given the name Interface 21 to imply the 21st century and launched under the license of Apache 2.0. Today, Spring is extensively used in the development of Java applications. It is liked by programmers and developers mainly due to its simplicity, testability, and loose coupling features.

3. What are other frameworks present in Java?

Java is one of the most popular, powerful, and versatile programming languages today. Java frameworks offer a base that enables developers to build software applications for specific platforms. So these frameworks essentially provide programmers with software and standards specific to the development and deployment of applications. There are more than 30 frameworks in Java; software developers use various Java frameworks today for application development. Some of the most popular ones are Spring, Play, Hibernate, Apache Struts, Dropwizard, Java Server Faces (JSF), Google Web Toolkit (GWT), Blade, Vaadin, and Grails. Out of all the Java frameworks, Spring is considered the most powerful and popular.

RELATED PROGRAMS