Skip to content
LightGuard edited this page Feb 11, 2012 · 4 revisions

Scopes and Contexts

Seam 2 provides a fixed set of contexts. These are listed in the table below. Any java type can be stored in a context. Session and conversation contexts require the values to be serializable. In Seam 2, the scope of a component is defined using one of the values of the ScopeType enumeration, e.g. @Scope(ScopeType.SESSION).

CDI provides a similar set of contexts. This set can be extended by additional scopes provided by portable extensions that implement the required SPI. In CDI, each scope has an associated annotation, which consists of the scope name plus Scoped, e.g. @SessionScoped.

CDI scopes are split into two groups:

  • Normal scopes – Implemented using dynamic proxies – the client receives a dynamic proxy for the contextual object. Everytime a method is invoked on a proxy, the container guarantees that the method is invoked on the actual contextual instance. Most of the built-in CDI scopes are normal scopes.
  • Pseudo-scopes – Client proxies are not required for pseudo-scopes. CDI provides single built-in pseudo-scope – @Dependent

As a result of using dynamic proxies for implementing normal scopes (every scope except for @Dependent), every managed bean bound to a normal scope is required to be proxyable. The exact definition of a proxyable bean is defined in the specification. This may be a problem for legacy applications that use unproxyable beans (e.g. an instance of java.lang.String bound to the session scope). The problem can be solved by:

  • making the bean proxyable (e.g. adding a non-private no-arg constructor or removing the final modifier of a class or method) or
  • creating a holder object that wraps the unproxyable object but is itself proxyable or
  • using @Dependent scope instead of a normal scope if possible

Corresponding scopes:

Seam 2 Scope CDI Scope
Event Request
Session Session
Stateless No direct corresponding scope. The stateless scope is using primarily for Stateless Session Beans (EJB) in Seam 2. A Stateless session bean can be bound to the @Dependent context in a CDI application.
No direct match Dependent – new instance for each injection point.
Page No direct match.
Conversation No direct corresponding scope. For long conversations, the corresponding scope is Conversation. For temporary conversations, the closest corresponding scope is Flash (but not the same). But there is no scope which would be automatically converted when stopping/starting a long-running conversation.
No direct match Conversation – covers only long-running conversations, where the boundaries are explicitly set by the programmer.