问题描述
在Scala Web应用程序中,惰性val的作用域是应用程序服务器的生存期,还是请求的作用域?
In a Scala web application, is lazy val scoped to the lifetime of the application server, or request scoped?
我认为这是针对每个请求的,但无法找到确定的答案,因此就是问题.
I assume it is per request, but have not been able to find a definitive answer, thus the question.
谢谢
推荐答案
lazy
是Scala功能,与Web应用程序编程无关.这意味着:在首次访问时仅评估一次.如果变量是每个请求创建的对象的一部分,则每个请求将对其进行一次惰性计算.
lazy
is a Scala feature, not related to web application programming. It means: evaluate only once on first access. If the variable is part of an object created per request, it will be lazily evaluated once per every request.
如果在应用程序范围的类(或object
)中声明了它,则一旦对其进行评估,它将在加载该类时保持其值不变(因此可能是WAR生存期).
If it is declared inside application-wide class (or object
), once evaluated it will keep its value as long as the class is loaded (so probably the WAR lifetime).
这篇关于Scala Web ::懒惰的Val范围/寿命的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!