Spring bean scopes with example

By | June 22, 2021

In this post, We will learn about the Spring bean scopes with example using a Demo Project.

In the Spring framework, we can create Spring beans in 6 inbuilt bean scopes and We can also create our custom bean scope as well.

Out of these six bean scopes, four scopes are available only if We use a web-aware ApplicationContext

singleton and prototype scopes are available in both core and web IOC containers.

Spring Bean Scopes Type

In Spring, the scope can be defined using spring bean @Scope annotation.

Let’s quickly look into all six inbuilt bean scopes available to use in the spring framework application context.

SCOPE DESCRIPTION
singleton (default scope) Single bean object instance per Spring IoC container. This Scope tells the Spring container to create and manage a single instance of bean class per Spring container. This single-created instance is stored in a cache and all subsequent requests return the cached instance.
prototype This scope is exactly opposite to the singleton Scope. it produces a new instance each and every time a Spring bean is requested.
request There is a single instance is created and available during the complete lifecycle of an HTTP request. This scope is only valid in web-aware Spring ApplicationContext.
session There is a single instance that is created and available during the complete lifecycle of an HTTP Session. This scope is only valid in web-aware Spring ApplicationContext.
application There is a single instance is created and available during the complete lifecycle of ServletContext.This scope is only valid in web-aware Spring ApplicationContext.
websocket There is a single instance is created and available during the complete lifecycle of WebSocket.This scope is only valid in web-aware Spring ApplicationContext.

In this demo project, We will learn about singleton  & prototype Scopes

pom.xml

Message.java

Java Class which is registered as Spring bean using @Component annotation in Spring Container.

Here message bean is registered under prototype scope using @Scope annotation

applicationContext.xml

We can also define the spring bean scope using the scope attribute of bean in  XML config. 

ClientTest.java

Client Program with the main method

If you run ClientTest.java as Java Application then it will give the below output: 

101 Hello World
null null

That’s all about Spring Bean scopes Example

You May Also Like:

Spring BeanFactory Container Example
Spring ApplicationContext Container Example
Annotation-based Configuration in Spring Framework Example
Spring Java-based Configuration Example
Spring Setter Dependency Injection Example
Spring @Autowired Annotation With Setter Injection Example
Spring Constructor based Dependency Injection Example
Spring @Autowired Annotation With Constructor Injection Example
Spring Autowiring byName & byType Example
getBean() overloaded methods in Spring Framework
Spring Inner bean example
Spring Dependency Injection with Factory Method
Spring Framework @Qualifier example
Injecting Collections in Spring Framework Example
Spring Bean Definition Inheritance Example
Spring JSR-250 Annotations with Example
Spring BeanPostProcessor Example
Spring JDBC Integration Example
Spring JDBC Annotation Example
Spring with Jdbc java based configuration example
Spring JDBC NamedParameterJdbcTemplate Example
How to call stored procedures in the Spring Framework?

If you have any feedback or suggestion please feel free to drop in below comment box.

Leave a Reply

Your email address will not be published.