The Spring bean scopes example introduces how to use and configure bean scopes in Spring application. Bean scope is used to decide which type of bean instance should be returned from Spring container. So when we create bean definitions, we cannot only do various dependencies and configurations, but also define the scope of the bean. This approach powerful and flexible in that you can choose the scope of the objects you create through configuration.
Other interesting posts you may like
There are multiple scopes:
singleton : (Default) Single bean definition for a single object instance per container.
prototype : Single bean definition for multiple instances.
request : Single bean definition for a single HTTP request.
session : Single bean definition for the HTTP session.
global-session: Single bean definition for the global HTTP session. Typically only valid when used in portlet context.
application: Single bean definition for the entire application bound to the lifecycle of a ServletContext
websocket: Scopes a single bean definition to the lifecycle of a WebSocket. Only valid in the context of a web-aware Spring ApplicationContext.
Note: Singleton and prototype are supported out of the box, and the default scope is singleton. The other scopes are valid only if you use a web-aware ApplicationContext.
In our Spring bean scopes example, we will discuss about Singleton Bean Scope and Prototype Bean Scope:
Singleton Bean Scope
Singleton bean means that only one instance of bean is created for a Spring container. All requests for beans with an id or ids matching that bean definition result in that one specific bean instance being returned by the Spring container.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package com.javabycode.spring.core.scope; public class Customer { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } } |
Configure spring bean as singleton in xml
1 2 3 4 5 |
<!-- default scope is singleton --> <bean class="com.javabycode.spring.core.scope.Customer"/> <!-- explicit singleton is equivalent --> <bean class="com.javabycode.spring.core.scope.Customer" scope="singleton"/> |
Demo singleton bean
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package com.javabycode.spring.core.scope; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MyCustomerApplication { public static void main(String... args) throws InterruptedException { ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml"); Customer customer1 = context.getBean(Customer.class); customer1.setName("Google Inc"); System.out.println("Customer name: " + customer1.getName()); //retrieve Customer bean again Customer customer2 = context.getBean(Customer.class); System.out.println("Customer name: " + customer2.getName()); } } |
Run the above main and get the output
1 2 |
Customer name: Google Inc Customer name: Google Inc |
Dig deeper: As you see the output, both of beans are really one bean. Because this single instance is stored in a cache of such singleton beans, and all subsequent requests and references for that named bean return the cached object.
Prototype Bean Scope
The prototype bean scope means that a bean definition is used to create multiple instances. This results in the creation of a new bean instance every time a request for that bean is made. Prototype scoped beans are mostly used for stateful beans whereas the singleton scope for stateless beans.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package com.javabycode.spring.core.scope; public class Product { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } } |
Configure spring bean as prototype
1 |
<bean class="com.javabycode.spring.core.scope.Product" scope="prototype"/> |
The bean is injected into another bean or you request it through a getBean() method call on the container.
Everytime we inject a bean into ano bean or request it through a getBean() method of the ApplicationContext, the container creates a new instance for that bean. It means that any subsequent request will also create a new bean.
Demo prototype bean
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package com.javabycode.spring.core.scope; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MyCustomerApplication { public static void main(String... args) throws InterruptedException { ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml"); //prototype bean Product product1 = context.getBean(Product.class); product1.setName("Google Inc"); System.out.println("Product name: " + product1.getName()); //retrieve Product bean again Product product2 = context.getBean(Product.class); System.out.println("Product name: " + product2.getName()); } } |
Run the above main and getting the output
1 2 |
Product name: Google Cloud Product name: null |
Configure Spring bean scope annotation
We can configure the bean scope using @Scope annotation instead of defining it in xml. This annotation takes a name parameter indicating which bean scope to use.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package com.javabycode.spring.core.scope; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; @Component @Scope("prototype") public class Product { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } } |
Don’t forget enable the
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- in case of using @Scope annotation --> <context:component-scan base-package="com.javabycode"/> </beans> |
Run the main above again, we are getting the same output
1 2 |
Product name: Google Cloud Product name: null |
That’s all on the Spring Bean Scopes example – Singleton vs Prototype
References
Spring Bean Scopes Doc
Download complete source code, click link below
spring-bean-scopes-example.zip (268 downloads)