Java Reference
In-Depth Information
Click here to view code image
@Specializes
public class MockAsynchronousService extends AsynchronousService {
... }
In this case, the MockAsynchronousService class will always be invoked instead
of the AsynchronousService class.
Usually, a bean marked with the @Specializes annotation is also an alternative and is
declared as an alternative in the beans.xml file. Such a bean is meant to stand in as a
replacement for the default implementation, and the alternative implementation automat-
ically inherits all qualifiers of the default implementation as well as its EL name, if it has
one.
Using Producer Methods, Producer Fields, and Disposer Methods in
CDI Applications
A producer method generates an object that can then be injected. Typically, you use pro-
ducer methods in the following situations:
• When you want to inject an object that is not itself a bean
• When the concrete type of the object to be injected may vary at runtime
• When the object requires some custom initialization that the bean constructor does
not perform
For more information on producer methods, see “Injecting Objects by Using Producer
Methods” in The Java EE 6 Tutorial: Basic Concepts .
A producer field is a simpler alternative to a producer method; it is a field of a bean that
generates an object. It can be used instead of a simple getter method. Producer fields are
particularly useful for declaring Java EE resources such as data sources, JMS resources,
and web service references.
A
producer
method
or
field
is
annotated
with
the
javax.enterprise.inject.Produces annotation.
Using Producer Methods
A producer method can allow you to select a bean implementation at runtime, instead of
at development time or deployment time. For example, in the example described in “ The
producermethods Example: Using a Producer Method To Choose a Bean Implement-
ation on page 271 , the managed bean defines the following producer method:
Search WWH ::




Custom Search