Java Reference
In-Depth Information
return new WidgetTwo();
return new WidgetX();
In the next example, a list of widgets is returned for use via a JSF view.
public List<Widget> getWidgets(){
return widgets;
The previous list of widgets can be displayed via a JSF view using the following markup:
<h:dataTable id="widgets" var="widget" value="#{widgetBean.widgets}">
<h:outputText value="#{widget.type}"/>
Producer fields are an alternative to producer methods. They are a field of a bean that can generate an object.
Producer fields are commonly used for the declaration of resources, but they can be used for the generation of other
objects as well.
A producer method can be used to generate an object that needs to be removed once it is no longer needed.
Much like a finalizer for a class, an object that has been injected via a producer method can contain a method that
is invoked when the injected instance is being destroyed. Such a method is known as a disposer method . To declare
a method as a disposer method, create a method defined by the same class as the producer method. The disposer
method must have at least one parameter, with the same type and qualifiers as the producer method. That parameter
should be annotated with @Disposes . In CDI 1.1, this technique can now be applied to producer fields as well.
Programmatic Lookup
In some cases, programmatic lookup of contextual instances may not be convenient. For instance, injection may not
be possible or convenient in the following circumstances:
If a bean's type or qualifiers vary dynamically
If there is no bean that satisfies the given type and/or qualifiers
If an application needs to iterate over all beans of a specified type
In these situations, you can inject a javax.enterprise.inject.Instance interface in order to gain access to
contextual references. For instance, in the following example, a contextual instance of AcmeBean is obtained. Once the
instance of the bean has been injected, its get method can be invoked to obtain the contextual reference.
@Inject Instance<AcmeBean> acmeBean;
AcmeBean ab = acmeBean.get();
Search WWH ::

Custom Search