Java Reference
In-Depth Information
@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