Java Reference
In-Depth Information
The @Requires annotation can be used to mark a class to be ignored by CDI if it does not meet the specified
required criteria. The @Requires annotation accepts a String -based fully qualified class name of the dependency
or dependencies. If the object is able to fulfill its dependencies, then it will be managed by CDI. For instance, the
following class requires the dependency of javax.persistence.EntityManager in order to be managed by CDI:
public class EmployeeFacade {
public EntityManager getEntityManager(){
Similarly to @Veto , the @Requires annotation can be placed on a package as well. If that package is unable to fulfill
the dependency that is denoted by @Requires , then all classes contained within that package will be unmanaged by CDI.
CDI brings forth the concept of producers. According to the specification, a producer method acts as a source of
objects to be injected, where the following are all true:
Objects to be injected aren't required to be bean instances.
Injected object types may vary at runtime.
An initialization method is required, separate from the bean constructor.
In a nutshell, a producer method allows generation of an injectable object. They are very useful in situations
where an application requires injection of an object that is not injectable or an object requires some custom
initialization that the bean's constructor does not take care of. Producer methods are annotated with the
javax.enterprise.inject.Produces annotation.
For instance, the following producer method can be called upon to obtain an injectable Widget bean, the type of
which may vary depending upon the given criteria:
public class WidgetBean implements {
private String widgetType = Widget.GENERIC;
private Widget selectedWidget;
public WidgetBean() {
public Widget getWidget() {
switch (widgetType) {
case Widget.PLASTIC:
return new WidgetOne();
case Widget.METAL:
Search WWH ::

Custom Search