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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.kkhindigyan.org</groupId> <artifactId>SpringBeanScopes</artifactId> <version>0.0.1-SNAPSHOT</version> <properties> <org.springframework.version>5.2.13.RELEASE</org.springframework.version> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${org.springframework.version}</version> </dependency> </dependencies> </project> |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
package com.kkhindigyan.model; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; @Component @Scope(scopeName = "prototype") public class Message { private Integer messageId; private String message; public Integer getMessageId() { return messageId; } public void setMessageId(Integer messageId) { this.messageId = messageId; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } } |
applicationContext.xml
We can also define the spring bean scope using the scope attribute of bean in XML config.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.kkhindigyan.model"></context:component-scan> <!-- <bean id="message" class="com.kkhindigyan.model.Message" scope="prototype"> </bean> --> </beans> |
ClientTest.java
Client Program with the main method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
package com.kkhindigyan.org; import org.springframework.context.ApplicationContext; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.kkhindigyan.model.Message; public class ClientTest { public static void main(String[] args) { ApplicationContext applicationContext = null; try { //Creating Instance of ApplicationContext Spring Container applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); //Asking Spring Container to return Spring bean with id "message" Object object = applicationContext.getBean("message"); //Covert Spring bean into your business Object Message message = (Message)object; message.setMessageId(101); message.setMessage("Hello World"); //Print Spring bean state System.out.println(message.getMessageId()+"\t"+message.getMessage()); Object object2 = applicationContext.getBean("message"); //Covert Spring bean into your business Object Message message2 = (Message)object2; System.out.println(message2.getMessageId()+"\t"+message2.getMessage()); } catch (Exception e) { e.printStackTrace(); }finally { if (applicationContext != null) ((AbstractApplicationContext) applicationContext).close(); } } } |
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.