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
public class ProjectsDaoImpl implements ProjectsDao {
@Stateless
@Remote(TasksMgmtRemote.class)
public class TasksMgmtEJB implements TasksMgmtRemote {
Beans can be simple POJOs, EJBs, Servlets, etc.
CDI Beans generalize behavior provided by larger frameworks
@Inject
private ProjectsDao projectsDao;
Type-safe, Java-based container injection
Container will match injection point with managed bean of requested type
Ambiguity error if container finds multiple classes satisfying requested type
@Stateless
@Remote(JobsMgmtRemote.class)
public class JobsMgmtEJB implements JobsMgmtRemote {
@Inject
private JobsDao jobDao; i //<== injection target
public class JobsDaoImpl implements JobsDao { //<== candidate 1 a bean of type
public class CdiDemoConfig {
@Produces
public JobsDao jobsDao(@CdiDemo EntityManager em) { //<== candidate 2 - a producer of type
Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001409:
Ambiguous dependencies for type JobsDao with qualifiers @Default
at injection point [BackedAnnotatedField]
@Inject private ejava.examples.cdiconfig.ejb.JobsMgmtEJB.jobDao
at ejava.examples.cdiconfig.ejb.JobsMgmtEJB.jobDao(JobsMgmtEJB.java:0)
Possible dependencies:
- Managed Bean [class ejava.examples.cdiconfig.dao.JobsDaoImpl] with qualifiers [@Any @Default],
- Producer Method [JobsDao] with qualifiers [@Any @Default] declared as [[BackedAnnotatedMethod]
@Produces public ejava.examples.cdiconfig.CdiDemoConfig.jobsDao(@CdiDemo EntityManager)]
Two or more sources of the target injection type
Ambiguity can be resolved through use of @Alternatives, @Priority, and qualifiers
@Inject @CdiDemo
private JobsDao jobDao;
@Produces
@CdiDemo
JobsDao jobsDao(@CdiDemo EntityManager em) {
@Annotation qualifier specializes injection type to remove ambiguity
package ejava.examples.cdiconfig;
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 CdiDemo {
}
java.lang.annoation
@Retention
how long the class is retained (default=CLASS)
RententionPolicy
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
<?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="fully qualified classname"/>
</scan>
<alternatives>
<class>...</class>
</alternatives>
</beans>
Can be blank or empty
Example shows XML alternative to using @Vetoed - which makes a bean unavailable for injection
Example shows enabling an @Alternative for application