When to use request, session, application scope in Spring application
Asked Answered
A

1

10

I have gone through the 5.5 Bean Scopes section in Spring, I would like to know some practical examples where we use the scopes for request, session and application scoped beans.

I have gone through this SO post --> Spring Bean Scopes but this just gives definition about the scopes in Spring.

Can someone please give an example in Banking Application or any Online Ticket Booking application or Online Shopping Application where we can use the scopes for request, session, application?

Article answered 20/5, 2015 at 18:46 Comment(2)
The definitions of the beans give a hint as to when to use them. If you need a bean that is unique and bound to a request, use the request scope. If you need a bean that should be shared across requests in a session, use the session scope. If you need a bean that should be shared across your whole application, use the singleton scope. This is similar to request, session, and servlet context attributes.Hazem
@SotiriosDelimanolis, Thanks a lot for responding. But can you please give some practical examples on this? I have discussed about this with some people who are working on Spring MVC but nobody ever used any of these scopes, not even prototype.Article
S
1

I will explain this step by step :

Scopes of a Spring Bean :

Scope Description
Singleton Single instance of a bean is created per container (by default).
Prototype New instance is created each time bean is requested.
Request New instance of a bean is created per each HTTP request.
Session New instance of a bean per each HTTP session.
Application Single instance of a bean is created per each ServletContext.
WebSocket Single instance of a bean per each WebSocket.

Example with a sample code :

I have created five beans to demonstrate all the scope's use - cases :

SingletonBean :

@Component
public class SingletonBean {
}

PrototypeBean :

@Component
@Scope("prototype")
public class PrototypeBean {
}

RequestBean :

@Component
@RequestScope
public class RequestBean {
}

SessionBean :

@Component
@SessionScope
public class SessionBean {
}

ApplicationBean :

@Component
@ApplicationScope
public class ApplicationBean {
}

Now, I have created one controller to show all the behaviors of the scopes :

@RestController
@Scope("prototype")
public class Resource {

    @Autowired
    private SingletonBean singletonBean;

    @Autowired
    private PrototypeBean protoTypeBean;

    @Autowired
    private RequestBean requestBean;

    @Autowired
    private SessionBean sessionBean;

    @Autowired
    private ApplicationBean applicationBean;

    @GetMapping("/testbeans")
    public String index() {
        return "<pre>" + singletonBean + "\n" + protoTypeBean + "\n" + requestBean + "\n" + sessionBean + "\n"
                + applicationBean + "\n" + "</pre>";
    }

}

When you hit the endpoint - http://localhost:8080/dweller/testbeans, you will get this output :

enter image description here

To check whether new instance of bean with request scope is created or not after, just refresh/send a new request.

enter image description here

Note: In addition to this, a new instance of a bean with prototype scope has also been created, the hashcode/address of the new instance is being shown in the above screenshot.

To check whether or not a new instance of a bean with session scope is created, just open an incognito window, and hit the same url. You will see now that a new instance is created for a bean with session scope.

enter image description here

To create a new instance of a singleton and application scope bean, you have to redeploy the application to see the changes.

I think this explanation is enough to get started with any project as I have explained the basics with a running code. I would recommend to start with a demo project (bank or ticket project that you have mentioned) by implementing all the scopes.

I'm adding a link to a real time internet banking system sequence diagram to implement the flow. Just understand the use-cases and try to see where you can add scopes.

Spear answered 21/9, 2022 at 8:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.