Spring MVC - Request Scoped Bean

[Last Updated: Apr 18, 2018]

The scope 'request' defines a single bean definition which lives within a single HTTP request. That means for each HTTP request a new bean instance is created. This scope is only valid in the context of a web-aware ApplicationContext.

This scope might be very useful in using helper objects across multiple beans through out a single request. This is particularly useful as compared to passing various parameters through a long chain of methods calls which sometimes becomes unmaintainable and it's difficult to add new parameters.


Since we are going to inject a 'request' scoped bean into a default 'singleton' scoped bean, we have to avoid narrower scope bean DI problem. For that we will use JSR 330 Provider approach. Here's a list of other solution.

Creating request scoped bean

public class EmployeeDetails implements Serializable {
  private Employee employee;
  private int lastYearSalary;
public class Employee {
  private String id;
  private String name;
  private String dept;

  public Employee(String id, String name, String dept) { = id; = name;
      this.dept = dept;

Injecting request bean to Controller

public class EmployeeController {
  private Provider<EmployeeDetails> employeeDetailsProvider;
  AppService appService;

  public String handler(@PathVariable("id") String employeeId) {
      return "employee-page";

Injecting request bean to other beans

public class AppService {
  private Provider<EmployeeDetails> employeeDetailsProvider;

  public void findEmployeeSalary() {
      EmployeeDetails employeeDetails = employeeDetailsProvider.get();
      Employee employee = employeeDetails.getEmployee();


To try examples, run embedded tomcat (configured in pom.xml of example project below):

mvn tomcat7:run-war

As seen above, all information of EmployeeDetails is there even though they were populated in different places by autowiring. That shows a single instance of EmployeeDetails was used through out the request.

Let's make a different request:

Example Project

Dependencies and Technologies Used:

  • spring-webmvc 5.0.5.RELEASE: Spring Web MVC.
  • javax.servlet-api 3.0.1 Java Servlet API
  • javax.inject 1: The javax.inject API.
  • JDK 1.8
  • Maven 3.3.9

Spring Mvc Request Scope Example Select All Download
  • request-scope-example
    • src
      • main
        • java
          • com
            • logicbig
              • example
          • webapp
            • WEB-INF
              • views

    See Also