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 producer 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
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
@Produces
@Chosen
@RequestScoped
public Coder getCoder() {
switch (coderType) {
case TEST:
return new TestCoderImpl();
case SHIFT:
return new CoderImpl();
default:
return null;
}
}
Here, getCoder becomes in effect a getter method, and when the coder
property is injected with the same qualifier and other annotations as
the method, the selected version of the interface is used.
@Inject
@Chosen
@RequestScoped
Coder coder;
Specifying the qualifier is essential: It tells CDI which Coder to
inject. Without it, the CDI implementation would not be able to choose
between CoderImpl, TestCoderImpl, and the one returned by getCoder
and would cancel deployment, informing the user of the ambiguous
dependency.
Using Producer Fields to Generate Resources
A common use of a producer field is to generate an object such as a JDBC
DataSource or a Java Persistence API EntityManager (see
Chapter 40, "Introduction to the Java
Persistence API," for more information). The object can then be managed
by the container. For example, you could create a @UserDatabase
qualifier and then declare a producer field for an entity manager as
follows:
@Produces
@UserDatabase
@PersistenceContext
private EntityManager em;
The @UserDatabase qualifier can be used when you inject the object
into another bean, RequestBean, elsewhere in the application:
@Inject
@UserDatabase
EntityManager em;
...
To minimize the reliance on resource injection, specify the producer field for the resource in one place in the application, and then inject the object wherever in the application you need it.
Using a Disposer Method
You can use a producer method or a producer field to generate an object
that needs to be removed when its work is completed. If you do, you need
a corresponding disposer method, annotated with a @Disposes
annotation. For example, you can close the entity manager as follows:
public void close(@Disposes @UserDatabase EntityManager em) {
em.close();
}
The disposer method is called automatically when the context ends (in
this case, at the end of the conversation, because RequestBean has
conversation scope), and the parameter in the close method receives
the object produced by the producer field.