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

1

8

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?

Piscine answered 10/3, 2012 at 23:32 Comment(0)
U
5

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:

U answered 11/3, 2012 at 0:2 Comment(2)
Thanks, that motivated me to search more about bad singleton designs and I read and watched some links on this topic that I found here in the forum.Piscine
What is the difference between the Singleton and ApplicationScope singleton annotations? (not the guarded singleton pattern, but the annotatons)? @UYolande

© 2022 - 2024 — McMap. All rights reserved.