Enterprise Java Development@TOPIC@
JSR-299 (CDI 1.0), JSR-346 (CDI 1.1)
Started as "Web Beans"
Derived from "Seam"
Original draft conflicted with Google and Spring JSR submission
JSR-330 (Java Injection)
Approved version refactored to account for JSR-330
Figure 105.1. Beans
@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
META-INF/beans.xml for EJBs and regular JARs
WEB-INF/beans.xml for WARs
Figure 105.5. beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans bean-discovery-mode="all" version="1.1"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd">
<scan>
<exclude name="info.ejava.examples.ejb.cdisales.dao.UserMgmtDAO"/>
</scan>
</beans>
Can be blank or empty
Example shows XML alternative to using @Veto - which makes a bean unavailable for injection