Spring Bean Scopes example – Singleton vs Prototype

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.

Configure spring bean as singleton in xml

Demo singleton bean

Run the above main and get the output

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.

Configure spring bean as 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

Run the above main and getting the output

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.

Don’t forget enable the element in spring xml configuration file. This enables annotation configuration and scans for beans in a particular package.

Run the main above again, we are getting the same output

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 (18 downloads)

If you found this useful, please share it with your friends

Leave a Comment

*

Please share it if you found this useful
Hide Buttons