Java Reference
In-Depth Information
Any POJO contained within the JAR archive can be a CDI bean. Generally you create an
instance of a bean by either injecting an instance or referencing a bean from a JSF page.
CDI will take care of acquiring an instance of the bean. One important feature to be aware
of is that CDI is much more flexible than either JSF-managed beans or EJBs. With POJO
beans you may have a constructor that takes arguments. Only one no-argument construct-
or may be provided, and its parameters must be other CDI beans (POJOs, JSF-managed
beans, or EJBs). In addition, CDI supports producer methods for creating beans in situ-
ations where you either need to keep a reference to the original bean or have complete con-
trol over the instantiation of the bean. We'll examine these different techniques for creating
beans as we proceed through the chapter. But first, let's examine the important concept of
component naming.
12.2.2. Component naming and EL resolution
Unlike other DI frameworks, CDI doesn't use string-based identifiers for beans. Instead it
relies on the Java type system so that DI is type-safe.
With unified EL expressions, there's no type information available. To solve this problem,
CDI provides an annotation, @Named , which must be placed on classes or producer meth-
ods (covered later). This annotation defines the name that can then be used in unified EL
expressions. The annotation is defined as follows:
package javax.inject;
@Qualifier
@Documented
@Retention(RUNTIME)
public @interface Named {
String value() default "";
}
This annotation can optionally take a value, which will be used as the name of the bean. If
no value is provided, the name of the class will be used instead. The @Named annotation
doesn't mark the class as being a CDI bean; it merely defines a name for the class so that it
can be called from a unified EL expression, as shown in the following listing..
Search WWH ::




Custom Search