Jun 8th, 2009, 01:45 AM
Bean Scopes and Business Logic
In a web based spring application, we can set the scopes for the beans.
Normally a session scope or a request scope. Not setting the scope means that the beans are application scoped.
When we have business logic inside our codes, what is the scope that we should put our bean?
Is session scope ok?
Or is request scope better or is application scope better?
I think we should put it in session scope because if there are a lot of people using the logic, then each person can have their own bean.
If we put it in the request scope, then we have to re-create the bean over and over again.
Is this correct?
Jun 8th, 2009, 02:51 AM
Your question could not be answered in general. However, your assumption that frequent re-creation of beans is not optimal is correct. So, if the bean in question is not connected to a user, then it should be application-scoped, not session-scoped. That way it needs to be created only once. Besides that it does not litter the session unnecessarily.
Jun 9th, 2009, 08:24 AM
May you be more specific - which kind of beans do you mean?`which role they play in your application? E.g. if they represent your services, then they should have singleton scope (BTW, the most often used scope).
Originally Posted by titiwangsa