Spring @Controller生命周期 [英] Spring @Controller lifecycle
本文介绍了Spring @Controller生命周期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是Spring MVC的新手,想知道如何处理请求,具体来说:
I am new to Spring MVC and would like to know how it handles requests, more specifically:
- 我想知道a
@控制器的生命周期与servlet的
相关? - 我也想更好
理解什么是最好的
多线程
环境的实践(例如在Servlets中,
是对于
可见的类属性,多个HTTP请求作为对象从池中重用
)
推荐答案
控制器(与任何spring bean一样)都有作用域。
A controller (as any spring bean) has a scope.
最好的控制器应该是 singleton
。在这种情况下,它非常像servlet,并且:
At best your controllers should be of scope singleton
. In that case it is very much like servlets, and:
- 它们只在应用程序上下文启动期间创建一次上下文被销毁)
- 您不应该使用任何实例变量(因为这不是线程安全的)
如果你的控制器范围是 request
或 session
,那么你可以有实例变量,则在每个新的请求/会话上创建控制器。
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.
这篇关于Spring @Controller生命周期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文