Enterprise Java Development@TOPIC@
Populate ENC
Lookup resource in ENC
Inject ENC resource
Inject resource without ENC
JavaEE/EJB-standard population/injection using class annotations
@javax.ejb.EJB
@javax.persistence.PersistenceContext
@javax.persistence.PersistenceUnit
@javax.annotation.Resource - general purpose dependency
JavaEE/EJB-standard population/injection using META-INF/ejb-jar.xml
<?xml version="1.0"?>
<ejb-jar
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/ejb-jar_3_2.xsd"
version="3.2">
<enterprise-beans>
<session>
<ejb-name>XMLConfiguredEJB</ejb-name>
...
</session>
...
</enterprise-beans>
JBoss-specific population/injection using META-INF/jboss-ejb3.xml
<?xml version="1.0"?>
<jboss:ejb-jar xmlns:jboss="http://www.jboss.com/xml/ns/javaee"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:s="urn:security"
xmlns:c="urn:clustering:1.0"
xmlns:p="urn:ejb-pool:1.0"
xsi:schemaLocation="http://www.jboss.com/xml/ns/javaee http://www.jboss.org/j2ee/schema/jboss-ejb3-2_0.xsd
http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/ejb-jar_3_1.xsd"
version="3.1"
impl-version="2.0">
<enterprise-beans>
<session>
<ejb-name>XMLConfiguredEJB</ejb-name>
...
</session>
...
</enterprise-beans>
</jboss:ejb-jar>
Populate ENC with value resource using ejb-jar.xml#env-entry
<env-entry>
<env-entry-name>val/value2</env-entry-name>
<env-entry-type>java.lang.String</env-entry-type>
<env-entry-value>from ejb-jar.xml</env-entry-value>
</env-entry>
Inject resource from ENC using @Resource
@Resource(name="val/value2")
private String value2="(class default value)";
Lookup ENC resource using JNDI or SessionContext
value2 = (String)jndi.lookup("java:comp/env/val/value2");
value2 = (String)ctx.lookup("val/value2");
Populate ENC with resource using Annotation. This also injects resource into the bean.
@Resource(lookup="java:jboss/datasources/ExampleDS", name="jdbc/ds2")
private DataSource ds2;
Populate ENC with ejb-jar.xml#resource-ref
<resource-ref>
<res-ref-name>jdbc/ds2</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<lookup-name>java:jboss/datasources/ExampleDS</lookup-name>
</resource-ref>
Populate ENC with jboss-ejb3.xml#resource-ref
<resource-ref>
<res-ref-name>jdbc/ds3</res-ref-name>
<jndi-name>java:jboss/datasources/ExampleDS</jndi-name>
</resource-ref>
Inject resource from ENC using annotations
@Resource(name="jdbc/ds3")
private DataSource ds3;
Lookup ENC resource using JNDI or SessionContext
ds2 = (DataSource)jndi.lookup("java:comp/env/jdbc/ds2");
ds2 = (DataSource)ctx.lookup("jdbc/ds2");
Inject resource without ENC using ejb-jar.xml#injection-target
<resource-ref>
<res-ref-name>jdbc/ds1</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<injection-target>
<injection-target-class>ejava.ejb.examples.encconfig.ejb.XMLConfiguredEJB</injection-target-class>
<injection-target-name>ds1</injection-target-name>
</injection-target>
<lookup-name>java:jboss/datasources/ExampleDS</lookup-name>
</resource-ref>
Inject resource without ENC using annotations
@Resource(lookup="java:jboss/datasources/ExampleDS")
private DataSource ds1;
Populate ENC with resource using Annotation. This also injects resource into the bean.
@PersistenceContext(unitName="enc-config", name="jpa/em2")
private EntityManager em2;
@PersistenceUnit(unitName="enc-config", name="jpa/emf2")
private EntityManagerFactory emf2;
Populate ENC with ejb-jar.xml#persistence-context-ref and persistence-unit-ref
<persistence-context-ref>
<persistence-context-ref-name>jpa/em2</persistence-context-ref-name>
<persistence-unit-name>enc-config</persistence-unit-name>
<injection-target>
<injection-target-class>ejava.ejb.examples.encconfig.ejb.XMLConfiguredEJB</injection-target-class>
<injection-target-name>em2</injection-target-name>
</injection-target>
</persistence-context-ref>
<persistence-unit-ref>
<persistence-unit-ref-name>jpa/emf2</persistence-unit-ref-name>
<persistence-unit-name>enc-config</persistence-unit-name>
<injection-target>
<injection-target-class>ejava.ejb.examples.encconfig.ejb.XMLConfiguredEJB</injection-target-class>
<injection-target-name>emf2</injection-target-name>
</injection-target>
</persistence-unit-ref>
Populate ENC with jboss-ejb3.xml#persistence-context-ref and persistence-unit-ref
<persistence-context-ref>
<persistence-context-ref-name>jpa/em3</persistence-context-ref-name>
<persistence-unit-name>enc-config</persistence-unit-name>
</persistence-context-ref>
<persistence-unit-ref>
<persistence-unit-ref-name>jpa/emf3</persistence-unit-ref-name>
<persistence-unit-name>enc-config</persistence-unit-name>
</persistence-unit-ref>
Inject resource from ENC using annotations
@PersistenceContext(name="jpa/em2")
private EntityManager em3;
@PersistenceUnit(name="jpa/emf3")
private EntityManagerFactory emf3;
Lookup ENC resource using JNDI or SessionContext
em2 = (EntityManager)jndi.lookup("java:comp/env/jpa/em2");
em2 = (EntityManager)ctx.lookup("jpa/em2");
emf2 = (EntityManagerFactory)jndi.lookup("java:comp/env/jpa/emf2");
emf2 = (EntityManagerFactory)ctx.lookup("jpa/emf2");
Inject resource without ENC using ejb-jar.xml#injection-target
<persistence-context-ref>
<persistence-context-ref-name>jpa/em1</persistence-context-ref-name>
<persistence-unit-name>enc-config</persistence-unit-name>
<injection-target>
<injection-target-class>ejava.ejb.examples.encconfig.ejb.XMLConfiguredEJB</injection-target-class>
<injection-target-name>em1</injection-target-name>
</injection-target>
</persistence-context-ref>
<persistence-unit-ref>
<persistence-unit-ref-name>jpa/emf1</persistence-unit-ref-name>
<persistence-unit-name>enc-config</persistence-unit-name>
<injection-target>
<injection-target-class>ejava.ejb.examples.encconfig.ejb.XMLConfiguredEJB</injection-target-class>
<injection-target-name>emf1</injection-target-name>
</injection-target>
</persistence-unit-ref>
Inject resource without ENC using annotations
@PersistenceContext(unitName="enc-config")
private EntityManager em1;
@PersistenceUnit(unitName="enc-config")
private EntityManagerFactory emf1;
Populate ENC with resource using Annotation. This also injects resource into the bean.
@Resource(lookup="java:/queue/test", name="jms/queue2")
private Queue queue2;
@Resource(lookup="java:/topic/test", name="jms/topic2")
private Topic topic2;
@Resource(lookup="java:/JmsXA", name="jms/cf2")
private ConnectionFactory cf2;
Populate ENC with ejb-jar.xml#resource-ref and resource-env-ref
<resource-ref>
<res-ref-name>jms/cf2</res-ref-name>
<res-type>javax.jms.ConnectionFactory</res-type>
<lookup-name>java:/JmsXA</lookup-name>
</resource-ref>
...
<resource-env-ref>
<resource-env-ref-name>jms/queue2</resource-env-ref-name>
<resource-env-ref-type>javax.jms.Destination</resource-env-ref-type>
<lookup-name>java:/queue/test</lookup-name>
</resource-env-ref>
<resource-env-ref>
<resource-env-ref-name>jms/topic2</resource-env-ref-name>
<resource-env-ref-type>javax.jms.Destination</resource-env-ref-type>
<lookup-name>java:/topic/test</lookup-name>
</resource-env-ref>
Populate ENC with jboss-ejb3.xml#resource-ref and resource-env-ref
<resource-ref>
<res-ref-name>jms/cf3</res-ref-name>
<jndi-name>java:/JmsXA</jndi-name>
</resource-ref>
<resource-env-ref>
<resource-env-ref-name>jms/queue3</resource-env-ref-name>
<jndi-name>java:/queue/test</jndi-name>
</resource-env-ref>
<resource-env-ref>
<resource-env-ref-name>jms/topic3</resource-env-ref-name>
<jndi-name>java:/topic/test</jndi-name>
</resource-env-ref>
Inject resource from ENC using annotations
@Resource(name="jms/queue3")
private Queue queue3;
@Resource(name="jms/topic3")
private Topic topic3;
@Resource(name="jms/cf3")
private ConnectionFactory cf3;
Lookup ENC resource using JNDI or SessionContext
queue2 = (Queue)jndi.lookup("java:comp/env/jms/queue2");
queue2 = (Queue)ctx.lookup("jms/queue2");
topic2 = (Topic)jndi.lookup("java:comp/env/jms/topic2");
topic2 = (Topic)ctx.lookup("jms/topic2");
cf2 = (ConnectionFactory)jndi.lookup("java:comp/env/jms/cf2");
cf2 = (ConnectionFactory)ctx.lookup("jms/cf2");
Inject resource without ENC using ejb-jar.xml#injection-target
<resource-ref>
<res-ref-name>jms/cf1</res-ref-name>
<res-type>javax.jms.ConnectionFactory</res-type>
<injection-target>
<injection-target-class>ejava.ejb.examples.encconfig.ejb.XMLConfiguredEJB</injection-target-class>
<injection-target-name>cf1</injection-target-name>
</injection-target>
<lookup-name>java:/JmsXA</lookup-name>
</resource-ref>
...
<resource-env-ref>
<resource-env-ref-name>jms/queue1</resource-env-ref-name>
<resource-env-ref-type>javax.jms.Queue</resource-env-ref-type>
<injection-target>
<injection-target-class>ejava.ejb.examples.encconfig.ejb.XMLConfiguredEJB</injection-target-class>
<injection-target-name>queue1</injection-target-name>
</injection-target>
<lookup-name>java:/queue/test</lookup-name>
</resource-env-ref>
<resource-env-ref>
<resource-env-ref-name>jms/topic1</resource-env-ref-name>
<resource-env-ref-type>javax.jms.Topic</resource-env-ref-type>
<injection-target>
<injection-target-class>ejava.ejb.examples.encconfig.ejb.XMLConfiguredEJB</injection-target-class>
<injection-target-name>topic1</injection-target-name>
</injection-target>
<lookup-name>java:/topic/test</lookup-name>
</resource-env-ref>
Inject resource without ENC using annotations
@Resource(name="jms/queue3")
private Queue queue3;
@Resource(name="jms/topic3")
private Topic topic3;
@Resource(name="jms/cf3")
private ConnectionFactory cf3;
Populate ENC with ejb-jar.xml#ejb-local-ref
<ejb-local-ref>
<ejb-ref-name>ejb/ejb2</ejb-ref-name>
<ejb-ref-type>Session</ejb-ref-type>
<local>ejava.ejb.examples.encconfig.ejb.InjectedEJB</local>
<ejb-link>InjectedEJB</ejb-link>
</ejb-local-ref>
Inject resource from ENC using annotations
@EJB(name="ejb/ejb2")
private InjectedEJB ejb2;
Lookup ENC resource using JNDI or SessionContext
ejb2 = (InjectedEJB)jndi.lookup("java:comp/env/ejb/ejb2");
ejb2 = (InjectedEJB)ctx.lookup("ejb/ejb2");
Inject resource without ENC using ejb-jar.xml#injection-target
<ejb-local-ref>
<ejb-ref-name>ejb/ejb1</ejb-ref-name>
<ejb-ref-type>Session</ejb-ref-type>
<local>ejava.ejb.examples.encconfig.ejb.InjectedEJB</local>
<ejb-link>InjectedEJB</ejb-link>
<injection-target>
<injection-target-class>ejava.ejb.examples.encconfig.ejb.XMLConfiguredEJB</injection-target-class>
<injection-target-name>ejb1</injection-target-name>
</injection-target>
</ejb-local-ref>
Inject resource without ENC using annotations
@EJB
private InjectedEJB ejb1;