File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Spring and the fly likes Servlet scopes vs. bean scopes in Spring Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Servlet scopes vs. bean scopes in Spring" Watch "Servlet scopes vs. bean scopes in Spring" New topic

Servlet scopes vs. bean scopes in Spring

Misi Nyilas

Joined: Nov 18, 2008
Posts: 3
I have some difficulties to map scopes used for servlets (application/session/request/private) to scopes used in Spring(singleton/prototype/request/session/global session). Can anyone offer me some public source where such a comparison/mapping has been described?
Just a few examples: is singleton the same as application scope used for servlets? If yes, what means global session? What are custom created scopes in Spring and how could be mapped to terms used for servlets? Are the request and session scopes in Spring equivalent to those ones used for servlets ore they are just similar? In latter case what are the differences?
I know the terms used in JSP, but I'm a begginner in Spring.
Thanks in advance!
Mark Spritzler

Joined: Feb 05, 2001
Posts: 17276

Spring scopes, Session and Request are for HttpSession and HttpRequests. However, they only work in the web environment. If you are running outside a web container it will default back to Singleton.

Singleton means one and only one instance inside the ApplicationContext. Now in the web environment with SPring MVC, there are actually two ApplicationContext's created. One called the parent ApplicationContext which holds your "backend" business logic, service/repository beans. In the second the Web ApplicationContext would have Controllers, ViewResolvers, MappingAdapters and Handlers. The Web ApplicationContext has access to beans in the parent one, but the parent cannot see the web one.

You can create your own custom scopes and they will mean what you want them to mean. An example say would be Thread scope, where beans get created per Thread. But you have to implement that work.

Prototype, is everytime you call getBean() you get a brand new instance.

I purposefully removed the need to think in terms of servlets because while two Spring scopes match in the web environment. The subject of Spring scopes is a different subject.

Hope that helps


Perfect World Programming, LLC - iOS Apps
How to Ask Questions the Smart Way FAQ
subject: Servlet scopes vs. bean scopes in Spring
It's not a secret anymore!