Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

JSF Scopes Tutorial - CDI Singleton Pseudo-Scope

DZone's Guide to

JSF Scopes Tutorial - CDI Singleton Pseudo-Scope

Get some background on the singleton pseudo-scope in CDI.

· Java Zone ·
Free Resource

Verify, standardize, and correct the Big 4 + more– name, email, phone and global addresses – try our Data Quality APIs now at Melissa Developer Portal!

The singleton pseudo-scope ensures a single instance of the bean. 

Image title

Besides the dependent pseudo-scope, we also have the singleton pseudo-scope. As its name suggests, a bean annotated with @Singleton(javax.inject.Singleton) is instantiated only once. This scope is available only in CDI and is a non-contextual scope. All CDI scopes, except this one (and@Dependent), are known as normal scopes.

A normal scope is a scope annotated with @NormalScope. These are contextual scopes having a client proxy. Contextual scopes implements Contextual interface which provides operations to create and destroy contextual instances of a certain type (create() and destroy()). During create() and destroy() theContextual interface uses the CreationalContext operations, push() and release(). Contextual instances with a particular scope of any contextual type are obtained via Context interface operations (e.g. get()).

A pseudo-scope is a scope annotated with @Scope. So, dependent scope is a pseudo-scope. This is a non-contextual scope with no client proxy. Well, the "no client proxy" part should be highlighted, ... and it is! CDI managed beans can be successfully used in JSF, but there is a problem with CDI managed beans annotated with@Singleton. They don't use proxy objects! Since there is a direct reference instead of a proxy we have some serialization issues. When singleton scoped beans are injected into client beans, the client beans will get a direct reference to the injected bean. So, if client bean is Serializable (e.g. SessionScoped) it must ensure that the injected singleton bean serialization is accomplished correctly.

 In order to keep the singleton state, we need to:

  • Have the singleton bean implement writeResolve() and readReplace() (as defined by the Java serialization specification),

  • Make sure the client keeps only a transient reference to the singleton bean, or

  • The client keeps a reference of type Instance<X> where X is the bean type of the singleton bean.

Fortunately, we can avoid @Singleton and still obtain the desired functionality. The answer lies in application scope:

@ApplicationScoped for CDI (javax.enterprise.inject.ApplicationScoped)

JSR 229 specification - The CDI application scope which guarantee that the class is instantiated only once. This is preferable because is much simpler to develop, test and maintain. So, when CDI is available, use this for having a singleton "surrogate" that provides a single instance and application scoped data.

@ApplicationScoped for JSF (javax.faces.bean.ApplicationScoped)

JSR 314 specification - The JSF application scope which guarantee that the class is instantiated only once - JSF as framework guarantee that only one instance will be created and reused during web application's lifetime. Again, this is preferable because is much simpler to develop, test and maintain.

Developers! Quickly and easily gain access to the tools and information you need! Explore, test and combine our data quality APIs at Melissa Developer Portal – home to tools that save time and boost revenue. Our APIs verify, standardize, and correct the Big 4 + more – name, email, phone and global addresses – to ensure accurate delivery, prevent blacklisting and identify risks in real-time.

Topics:
jsf ,singletons

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}