As there are 5 types of scopes a bean can have, I know how different they are but I want to know what bean scope is used in real web application projects. So please tell what scope for a bean you had decided to use while developing the applications.
They all have different purposes and can be used within the same application, except global session beans, which are only valid for portlet applications.
Stateless services that you would only configure once for the entire application (such as DAOs/repositories) would have singleton scope. Stateful services that you want to create a new instance of for every object that depend on them should have the prototype scope. Random number generators are a good example.
If you want a bean that is associated with a single request you would use request scope, and if the bean must be used across multiple requests within a single user session, you would use session scope. For instance, a ShoppingCart or User can be associated with a session.
In Spring 5, we don't call global session scope. Instead , we call it application scope.
Application scope means the bean lives in a servlet context. One servlet context can have multiple application context.
A singleton lives in one application context.
A bean of application scope can be shared with multiple application contexts.
So, application scoped bean can live longer than a singleton.
I guess if you want to build a real web (servlet) application and you want a bean to be shared among multiple application context, use application scoped bean.
Skool. Stay in. Smartness. Tiny ad:
Devious Experiments for a Truly Passive Greenhouse!