Enterprise Java Development@TOPIC@

Chapter 106. CDI Injection

106.1. Producer Field
106.2. Producer Method
106.3. Other Producers
106.4. @Any Instance
106.5. Bean Types
106.6. Named Beans
106.7. Summary
    public class SchedulerResources {

        @PersistenceContext(unitName="jndidemo")-->-+
        @Produces                                   |
        @JndiDemo                                   |
 +-<----public EntityManager em; <------------------+
 |  
 |  @Stateless
 |  public class TrainSchedulerEJB 
 |      extends SchedulerBase implements TrainSchedulerRemote {
 |      
 |      @Inject @JndiDemo
 `----->private EntityManager em;
    public class DAOFactory {

        @PersistenceContext(unitName="ejbcdi-sales")
        @Produces
        public EntityManager em;
        
        @Produces
        @Dependent
 +-<----public UserMgmtDAO getUserMgmtDAO(EntityManager em, Validator validator) {
 |          UserMgmtDAO dao = new UserMgmtDAO();
 |          dao.setEntityManager(em);
 |          dao.setValidator(validator);
 |          return dao;
 |      }
 |  
 |  @Stateless
 |  public class UserMgmtEJB implements UserMgmtLocal {
 |      @Inject
 `----->private UserMgmtDAO dao;

  • Injection source populated from a JNDI lookup


  • Injection source populated from hard coded value. Could be changed to method.

        @EJB(lookup="java:app/jndiDemoEJB/CookEJB!ejava.examples.jndidemo.ejb.CookLocal")

        @Produces                                            |
        @Cook                                                |
 +---<--public Scheduler cook; <-----------------------------+
 |  
 |  @Stateless
 |  public class TrainSchedulerEJB 
 |      @Inject @Any
 `----> protected Instance<Scheduler> anyCook;
        protected Scheduler cook2;      |
                    +-------------------+
                    v 
        cook2 = anyCook.select(new CookQualifier()).get();
public class CookQualifier extends javax.enterprise.util.AnnotationLiteral<Cook>{}
@Named

public class JPASchedulerDAOImpl extends JPADAOBase<Task> implements SchedulerDAO {
@Typed({SchedulerDAO.class, JPASchedulerDAOImpl.class})

public class JPASchedulerDAOImpl extends JPADAOBase<Task> implements SchedulerDAO {
@Named("sellerController")

@ConversationScoped
public class SellerController implements Serializable {

<h3>Sell Products (#{sellerController.seller.name})</h3>