Java singleton class vs JSF application scoped managed bean - differences?

那年仲夏 提交于 2019-11-26 23:28:29

问题


Is there a difference using a singleton class and an application scoped managed bean to hold application data?

I need to lookup certain JNDI ressources such as remote bean interfaces and therefore I wrote myself a singleton to cache my references and only allow single references. (ServiceLocator)

I opened my website in two different browsers and that singleton got only initialized once. So I assume its application scope?

Any other benefits of a application scope managed bean then being able to access its properties in jsf?


回答1:


Singletons are not unit testable nor abstractable nor extendable. Singletons are also unnecessarily complex to reliably create and maintain if your sole purpose is to have application scoped data (at least, if you really want a fullworthy singleton for it for some reason -most starters don't even exactly understand what a singleton is supposed to be).

"Just create one" like an application scoped managed bean is much simpler to develop, test and maintain. JSF as framework will guarantee that only one instance will be created and reused during web application's lifetime.

See also:

  • Singleton vs Just Create one
  • How to choose the right bean scope?


来源:https://stackoverflow.com/questions/9651132/java-singleton-class-vs-jsf-application-scoped-managed-bean-differences

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