Scala web :: lazy val scope/lifetime

2019-09-07 08:18发布

问题:

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.

Thanks

回答1:

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.

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).