Enterprise Java Development@TOPIC@
This chapter focuses on injecting relevant resources into an EJB using modern injection techniques. From the points made in the previous chapter -- you know that there are more tedious and verbose techniques from the older EJB specs that pre-date @Annotations and ease-of-use enhancements. They will not be included here.
Inject a Resource to locate information from Container
private @Resource SessionContext ctx;
ctx.lookup("...")
ctx.getCallerPrincipal()
ctx.getRollbackOnly()
Inject a pool of database connections
From JNDI lookup using 100% annotation-based
@Resource(lookup="java:jboss/datasources/ExampleDS")
private DataSource ds1;
From ENC using annotations
@Resource(name="jdbc/ds3")
private DataSource ds3;
Inject access to the Persistence Unit
From persistence-unit name using 100% annotation-based
@PersistenceContext(unitName="enc-config")
private EntityManager em1;
@PersistenceUnit(unitName="enc-config")
private EntityManagerFactory emf1;
From ENC using annotations
@PersistenceContext(name="jpa/em3")
private EntityManager em3;
@PersistenceUnit(name="jpa/emf3")
private EntityManagerFactory emf3;
Use unitName
and not name
when specifying the name of the persistence unit. name
is for the ENC name.
Inject access to other EJBs
From type matching using 100% annotations
@EJB
private InjectedTypedLocal ejb9a;
@EJB
private InjectedTypedRemote ejb9b;
@EJB
private InjectedEJB ejb1;
Container is able to locate a single component that implements injected variable type
Last example is an example of a No Interface, @LocalBean EJB
No Interface is common technique for "helper" EJBs that implement a container-managed boundary (e.g., security @RunAs or transaction REQUIRES_NEW)
From type matching with Local/Remote ambiguities
@EJB
private InjectedTyped ejb10;
//DeploymentUnitProcessingException: WFLYEJB0406:
//No EJB found with interface of type 'ejava.ejb.examples.encconfig.ejb.InjectedTyped'
@EJB(beanInterface=InjectedTypedLocal.class)
private InjectedTyped ejb10a;
@EJB(beanInterface=InjectedTypedRemote.class)
private InjectedTyped ejb10b;
Example shows @Local and @Remote extending from common InjectedTyped interface that presents a problem trying to use the base interface type.
From JNDI lookup using 100% annotations
lookup is referencing a specific component using Portable JNDI Syntax
Global definition valid locally from outside the application. EAR may or may not apply.
java:global[/application name]/module name/enterprise bean name[/interface name]
@EJB(lookup="java:global/enc-config-example-ejb/InjectedEJB!ejava.ejb.examples.encconfig.ejb.InjectedEJB")
private InjectedEJB ejb3;
@EJB(lookup="java:global/enc-config-example-ejb/InjectedEJB")
private InjectedEJB ejb6;
Application definition valid locally from within the application
java:app[/module name]/enterprise bean name[/interface name]
@EJB(lookup="java:app/enc-config-example-ejb/InjectedEJB!ejava.ejb.examples.encconfig.ejb.InjectedEJB")
private InjectedEJB ejb4;
@EJB(lookup="java:app/enc-config-example-ejb/InjectedEJB")
private InjectedEJB ejb7;
Module definition valid locally with within same module java:module/enterprise bean name/[interface name]
java:module/enterprise bean name/[interface name]
@EJB(lookup="java:module/InjectedEJB!ejava.ejb.examples.encconfig.ejb.InjectedEJB")
private InjectedEJB ejb5;
@EJB(lookup="java:module/InjectedEJB")
private InjectedEJB ejb8;
From ENC using annotations
@EJB(name="ejb/ejb2")
private InjectedEJB ejb2;