Enterprise Java Development@TOPIC@

Chapter 105. Context and Dependency Injection (CDI) JSR-299

105.1. History
105.2. Injection Concept
105.3. CDI Activation
105.4. Summary

  • 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


  • @Annotation qualifier specializes injection type to remove ambiguity


  • 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