Java单例类与JSF应用程序范围内的托管bean-有区别吗? [英] Java singleton class vs JSF application scoped managed bean - differences?

查看:107
本文介绍了Java单例类与JSF应用程序范围内的托管bean-有区别吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用单例类和应用程序范围的托管Bean来保存应用程序数据是否有所不同?

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

我需要查找某些JNDI资源,例如远程bean接口,因此我给自己写了一个单例来缓存我的引用,并且只允许单个引用. (ServiceLocator)

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?

应用程序范围托管Bean的其他好处是,然后能够在jsf中访问其属性吗?

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

推荐答案

单子不可进行单元测试,抽象或扩展.如果您的唯一目的是拥有应用程序范围的数据(至少,如果出于某些原因,您真的真的想要一个值得拥有的单身人士),那么单身人士的创建和维护也就不必要地变得复杂,因为大多数初学者甚至都不会完全了解单身人士应该是什么).

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

像创建应用程序范围的受管bean一样,仅创建一个"就更易于开发,测试和维护.作为框架的JSF将确保在Web应用程序的生存期内仅创建和重用一个实例.

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

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

这篇关于Java单例类与JSF应用程序范围内的托管bean-有区别吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆