Spring @Controller lifecycle

会有一股神秘感。 提交于 2019-12-13 02:02:48

问题


I am new to Spring MVC and would like to know how it handles requests, more specifically:

  1. I would like to know how a Spring @Controller's life cycle relates to that of a Servlet?
  2. I would also like to better understand what are the best practices for multi-threaded enviornments (e.g. like in Servlets, are class attributes visible to multiple HTTP requests as objects are reused from the pool)?

回答1:


A controller (as any spring bean) has a scope.

At best your controllers should be of scope singleton. In that case it is very much like servlets, and:

  • they are created only once, during the application context startup (and destroyed when the context is destroyed)
  • you should not use any instance variables (as this is not thread-safe)

If your controller scope is request or session, then you can have instance variables, and an instance of the controller is created on each new request/session.



来源:https://stackoverflow.com/questions/5667727/spring-controller-lifecycle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!