@PortletLifecycleScoped for CDI in GateIn 3.6.0.Final
Posted by kenfinni in GateIn on Jul 16, 2013 8:41:04 AMWith the release of GateIn 3.6.0.Final we introduced some new CDI scopes to ease its use within a portlet. In this first blog on CDI scopes for GateIn, we will cover @PortletLifecycleScoped. This scope can be used with GenericPortlets, Portlet Filters, or JSF Portlets.
This scope lives for a single Portlet Request lifecycle and is destroyed on completion, meaning that there is no way to guarantee a subsequent Render will generate the same portlet content. The following table illustrates when the context for the scope is activated (A), destroyed (X), or when its already present (P):
Portlet Trigger | Action Start | Action Complete | Event Start | Event Complete | Render Start | Render Complete | Resource Start | Resource Complete |
---|---|---|---|---|---|---|---|---|
Portlet Action | A | P | P | P | P | X | ||
Portlet Event | A | P | P | X | ||||
Portlet Render | A | X | ||||||
Portlet Resource | A | X |
An important distinction is that any Beans created within the scope context as part of a Portlet Action, Portlet Event or Portlet Render are not visible within a Portlet Resource call, and vice versa. Each ResourceRequest will receive a completely empty set of Beans that have been constructed, including calls to any @PostConstruct, but that have had no code executed against them. Any changes to Beans within a ResourceRequest is not retained in any way. This situation is perfect for making Ajax calls from a portlet, so long as the data you're relying on perform the work is accessible from non @PortletLifecycleScoped beans.
To begin using the new scope, simply add the following dependency into your portlet project pom.xml:
<dependency> <groupId>org.gatein.api</groupId> <artifactId>gatein-api</artifactId> <version>1.0.0.Final</version> <scope>provided</scope> </dependency>
We mark the dependency as provided since GateIn will automatically link our deployment to the latest API dependency at runtime.
With the above dependency we can then create a bean such as:
@PortletLifecycleScoped public class LifecycleBean { ... }
As far as CDI is concerned, @PortletLifecycleScoped is a bean scope like all those that it provides. This enables us to inject @RequestScoped, @ConversationScoped, @SessionScoped or @ApplicationScoped beans into a @PortletLifecycleScoped bean, or vice versa.
Hope this and the scope we cover in the next blog will make developing CDI portlets with GateIn that much easier!
Comments