Spring Bean Scope using Annotation – Singleton, Prototype, Request, Session, Global Session, Application

The tutorial shows you the way to implement Bean Scopes (Singleton, Prototype, Request and Session) using Annotation @Scope. Except Singleton and Prototype Scope, the others are only valid in the context of a web-aware Application.

Related Article: Spring Bean Scope – Singleton and Prototype

I. Technology

– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE
– Spring Boot: 1.4.0.RELEASE

II. Overview
1. Goal

To build a simple application using @Scope annotation that shows you the way singleton, prototype, request, session act and the difference between them.
Beside, we also have a lean introduction of globalSession and application scope.

2. Project Structure

Customer class has four @Autowired members:
+ Name: singleton
+ Language: prototype
+ Address: request
+ Age: session
WebController is a @RestController which has some request mappings to test data for each scope.
application.properties contains configuration for session cookie and timeout (in case of example for session scope).

3. Step to do

– Create Spring Boot project
– Create Classes for Beans
– Create a WebController
– Add configuration to application.properties
– Run Spring Boot Application & Enjoy Result

III. Practice
1. Create Spring Boot project

– Open Spring Tool Suite, on Menu, choose File -> New -> Spring Starter Project, then fill each fields:

Click Next, in Web: choose Web:

Click Finish. Spring Boot project will be created successfully.

2. Create Classes for Beans

We annotate the classes which bean has scope by @Scope(value = [scope], proxyMode = ScopedProxyMode.TARGET_CLASS)
– Proxy Mode TARGET_CLASS: is used for creating AOP proxy of the bean. We must do it when injecting short lived bean scope (request, session…) to long lived bean scope (singleton, prototype) or injecting prototype to singleton scope.
– We have some type of scope:
+ singleton: only one instance is created (default scope)
+ prototype: new instance is created everytime prototype bean is referenced.
+ request: one instance for a single HTTP request.
+ session: one instance for an HTTP Session
+ globalSession: one instance for a global HTTP Session. Typically only valid when used in a Portlet context.
+ application: Scopes a single bean definition to the lifecycle of a ServletContext (Only valid in the context of a web-aware Spring ApplicationContext).

In this tutorial, we don’t use globalSession and application scope.
Now, under package bean, create necessary classes.

Content of Name.java:

Content of Language.java:

Content of Address.java:

Content of Age.java:

Content of Customer.java:

3. Create a WebController

Under package controller, create class WebController.
Content of WebController.java:

4. Add configuration to application.properties

Open application.properties under src/main/resources, add configuration:

We set session timeout every 10 seconds to check the case of session scope.

5. Run Spring Boot Application & Enjoy Result

– Config maven build:
clean install
– Run project with mode Spring Boot App
– Check results:

After running project, we can see that system has the message:

This case occurs to singleton scope bean.

Request 1

Request 1-1
Request 2
System shows:

Request 2-1
System shows:

Request 3
System shows:

Request 3-1
System shows:

Request 4
System shows:

Request 4-1
System doens’t show anything more.

Wait more than 10 seconds (timeout)
Request 4-2
System shows:

IV. Source Code


Related Posts

Got Something To Say:

Your email address will not be published. Required fields are marked *