Enterprise Java Development@TOPIC@
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 field used as injection source
Useful when no behavior is required
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 type was not ambiguous - no @Qualifier required
Method called by container
Method declares parameters for what it needs to build product
Method parameters automatically injected
@Dependent allows injection point to determine scope for the bean (e.g., request, session, etc.)
Figure 106.1. JNDI Resource Lookup
@Resource(lookup="java:jboss/datasources/ExampleDS")
@Produces
@JndiDemo
public DataSource ds;
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>{}
Allows bean to hold onto all implementations of type
Bean can select from different implementations at runtime
@Named
public class JPASchedulerDAOImpl extends JPADAOBase<Task> implements SchedulerDAO {
Assignable to four (4) different types
JPASchedulerDAOImpl
JPADAOBase<Task>
SchedulerDAO
java.lang.Object
@Typed({SchedulerDAO.class, JPASchedulerDAOImpl.class})
public class JPASchedulerDAOImpl extends JPADAOBase<Task> implements SchedulerDAO {
@Type reduces number of types allowed
JPASchedulerDAOImpl
SchedulerDAO
@Named("sellerController")
@ConversationScoped
public class SellerController implements Serializable {
<h3>Sell Products (#{sellerController.seller.name})</h3>
Used for document expression languages to reference intended bean