Win a copy of Emmy in the Key of Code this week in the General Computing 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 ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

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

 
Ranch Hand
Posts: 240
2
  • 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: 10750
229
  • 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 Hand
Posts: 1769
12
  • 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.
 
You may have just won ten million dollars! Or, maybe a tiny ad.
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!