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
For more information on producer methods, see Injecting Objects by Using Producer Methods.
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 Implementation, the managed bean defines the following producer method:
@Produces @Chosen @RequestScoped public Coder getCoder(@New TestCoderImpl tci, @New CoderImpl ci) { switch (coderType) { case TEST: return tci; case SHIFT: return ci; default: return null; } }
The javax.enterprise.inject.New qualifier instructs the CDI runtime to instantiate both of the coder implementations and provide them as arguments to the producer method. 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. 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; ...
The producerfields Example: Using Producer Fields to Generate Resources shows how to use producer fields to generate an entity manager. You can use a similar mechanism to inject @Resource, @EJB, or @WebServiceRef objects.
To minimize the reliance on resource injection, specify the producer field for the resource in one place in the application, then inject the object wherever in the application you need it.
Using a Disposer Method
You can use a producer method 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, if you used a producer method instead of a producer field to create the entity manager, you would create and close it as follows:
@PersistenceContext private EntityManager em; @Produces @UserDatabase public EntityManager create() { return em; } 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 method, create.