Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet scopes vs. bean scopes in Spring

 
Misi Nyilas
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic