-->@Named
public class JPASchedulerDAOImpl extends JPADAOBase<Task> implements SchedulerDAO {
@Stateless
public class CookEJB extends SchedulerBase implements CookLocal {
Beans can be simple POJOs, EJBs, Servlets, etc.
CDI Beans generalize behavior provided by larger frameworks
Type-safe, Java-based container injection
Container will match injection point with managed bean of requested type
Error if container finds multiple classes satisfying requested type
Figure 105.3. Qualifiers and Qualified Injection
@Inject @Cook
protected Scheduler cook;
@Stateless
@Cook
public class CookEJB extends SchedulerBase implements CookLocal {
@Annotation qualifier specializes injection type to remove ambiguity
Figure 105.4. Qualifier Annotations
package ejava.examples.jndidemo;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import static java.lang.annotation.RetentionPolicy.*;
import static java.lang.annotation.ElementType.*;
import javax.inject.Qualifier;
@Qualifier
@Retention(RUNTIME)
@Target({METHOD, FIELD, PARAMETER, TYPE})
public @interface Cook {
}
java.lang.annoation
@Retention
how long the class is retained (default=CLASS)
RententionPolict
CLASS - annotation recorded in class file but not available at runtime
RUNTIME - annotation recorded in class and available at runtime
SOURCE - annotation discarded by compiler
@Target
kind of Java construct the annotation applies to
ElementType
TYPE
FIELD
METHOD
PARAMETER
(more)
javax.inject (JSR-330)
@Inject
an injection target
@Named
string qualifier (for JSPs, etc. to reference bean using text)
@Qualifier
identifies qualifier annotations
javax.enterprise.inject (JSR-299)
@Produces
an injection source
@Any
no qualifiers
Instance<Bean>
runtime source of type Bean
Activated when beans.xml is located in appropriate location
Can be blank or empty
Example shows XML alternative to using @Veto - which makes a bean unavailable for injection