Win a copy of 97 Things Every Java Programmer Should Know this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
  • Piet Souris
  • salvin francis
  • fred rosenberger

what "scope" for the beans would you choose for an e-commerce application?

Ranch Hand
Posts: 240
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. singleton(default*)
2. prototype
3. request
4. session
5. global session

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.

Thanks in advance.
Saloon Keeper
Posts: 12024
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Ranch Foreman
Posts: 1898
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
    Bookmark Topic Watch Topic
  • New Topic