Search code examples
javasessionspring-mvcjavabeans

In Spring MVC, do I need to unbind bean objects bound to the session when I am done using the session scoped bean? If yes, how?


I am using few session scoped beans in the app context xml of my spring mvc app. I have seen invalidate() method in HTTPSession class which does the detaching thing. Do I need to call this method somewhere in my spring app also to free up the session from the session scoped beans when those beans are mo more in use? Or does Spring internally take care of this?


Solution

  • No, you don't need to call Session#invalidate() for session scoped beans as DispatcherServlet takes care of these things.

    It use 'org.springframework.web.context.request.RequestAttributes#registerDestructionCallback callback list functionality to register all these scoped beans.

    Link : http://grepcode.com/file/repo1.maven.org/maven2/org.springframework/spring-webmvc/2.5.6/org/springframework/web/servlet/DispatcherServlet.java#940