Spring Bean Scopes: Singleton & Prototype Scopes [2021]

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. 

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

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 the singleton. Still, this code sample can be used to set the scope to the singleton.

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.

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.

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

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 PG Diploma 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.

Land on Your Dream Job

UPGRAD AND IIIT-BANGALORE'S PG DIPLOMA IN SOFTWARE DEVELOPMENT
Learn More

Leave a comment

Your email address will not be published.

×