EJB 3.0 and env-entry injection

Version 2
    package org.acme;
    
    public class MyClass {
      @Resource(name="providerURL") String field;
    }
    
    <env-entry>
     <env-entry-name>providerURL</env-entry-name>
     <env-entry-type>java.lang.String</env-entry-type>
     <env-entry-value>ldap://localhost:389/</env-entry-value>
    </env-entry>
    

     

     

    OR

     

     

    package org.acme;
    
    public class MyClass {
      @Resource String field;
    }
    
    <env-entry>
     <env-entry-name>org.acme.MyClass/field</env-entry-name>
     <env-entry-type>java.lang.String</env-entry-type>
     <env-entry-value>ldap://localhost:389/</env-entry-value>
    </env-entry>
    

     

     

    OR

     

     

    package org.acme;
    
    public class MyClass {
      String field;
    }
    
    <env-entry>
     <env-entry-name>providerUrl</env-entry-name>
     <env-entry-type>java.lang.String</env-entry-type>
     <env-entry-value>ldap://localhost:389/</env-entry-value>
     <injection-target>
     <injection-target-class>org.acme.MyClass</injection-target>
     <injection-target-name>field</injection-target-name>
     </injection-target>
    </env-entry>
    

     

     

     

    Overriding defaults:

     

    @Resource int maxExemptions = 4;
    <env-entry>
        <env-entry-name>maxExemptions</env-entry-name>
        <env-entry-type>java.lang.Integer</env-entry-type>
        <env-entry-value>15</env-entry-value>
    </env-entry>
    
    

     

     

     

    If you download the entire source from CVS (jboss-head), there are examples in the ejb3 JUnit tests. Take a look at the enventry test. Here is an example bean and ejb-jar.xml

     

     

    @Stateless(name="TestEnvEntry")
    @Remote(TestEnvEntry.class)
    @RemoteBinding(jndiBinding="TestEnvEntry")
    public  class TestEnvEntryBean   implements TestEnvEntry
    {
    
      private  static final Logger log = Logger.getLogger(TestEnvEntryBean.class);
    
      @Resource(name="maxExceptions") private int maxExceptions = 4;
    
      @Resource private int numExceptions = 3;
    
      private int minExceptions = 1;
    
      public int getMaxExceptions()  {  return maxExceptions;   }
    
      public int getNumExceptions() {  return numExceptions;   }
    
      public int getMinExceptions() {  return minExceptions;  }
    }
    

     

     

    <ejb-jar>
      <display-name>EnvEntry Tests</display-name>
      <enterprise-beans>
    
        <ejb-name>TestEnvEntry</ejb-name>
        <env-entry>
          <env-entry-name>maxExceptions</env-entry-name>
          <env-entry-type>java.lang.Integer</env-entry-type>
          <env-entry-value>15</env-entry-value>
        </env-entry>
        <env-entry>
          <env-entry-name>minExceptions</env-entry-name>
          <env-entry-type>java.lang.Integer</env-entry-type>
          <env-entry-value>5</env-entry-value>
          <injection-target>
            <injection-target-class>org.jboss.ejb3.test.enventry.TestEnvEntryBean</injection-target-class>
            <injection-target-name>minExceptions</injection-target-name>
          </injection-target>
        </env-entry>
        <env-entry>
          <env-entry-name>org.jboss.ejb3.test.enventry.TestEnvEntryBean/numExceptions</env-entry-name>
          <env-entry-type>java.lang.Integer</env-entry-type>
          <env-entry-value>10</env-entry-value>
        </env-entry>
    
        <ejb-name>ExtendedTestEnvEntry</ejb-name>
        <env-entry>
          <env-entry-name>maxExceptions</env-entry-name>
          <env-entry-type>java.lang.Integer</env-entry-type>
          <env-entry-value>14</env-entry-value>
        </env-entry>
        <env-entry>
          <env-entry-name>minExceptions</env-entry-name>
          <env-entry-type>java.lang.Integer</env-entry-type>
          <env-entry-value>6</env-entry-value>
          <injection-target>
            <injection-target-class>org.jboss.ejb3.test.enventry.ExtendedTestEnvEntryBean</injection-target-class>
            <injection-target-name>minExceptions</injection-target-name>
          </injection-target>
        </env-entry>
        <env-entry>
          <env-entry-name>org.jboss.ejb3.test.enventry.ExtendedTestEnvEntryBean/numExceptions</env-entry-name>
          <env-entry-type>java.lang.Integer</env-entry-type>
          <env-entry-value>11</env-entry-value>
        </env-entry>
    
      </enterprise-beans>
    </ejb-jar>

     

     

     

    --------------

    Here is an example.
    All three work but I have shown the first 2 in this example

     

    ejb-jar.xml

     

    <?xml version="1.0" encoding="UTF-8"?>
    <ejb-jar version="3.0"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee ../../../testapp/ejb-jar_3_0.xsd ">
      <enterprise-beans>
        <session>
          <ejb-name>com.suresh.ejb.test.HelloBean</ejb-name>
          <remote>com.suresh.ejb.test.Hello</remote>
          <ejb-class>com.suresh.ejb.test.HelloBean</ejb-class>
          <session-type>Stateless</session-type>
          <transaction-type>Container</transaction-type>
          <env-entry>
            <env-entry-name>com.suresh.ejb.test.HelloBean/urls</env-entry-name>
            <env-entry-type>java.lang.String</env-entry-type>
            <env-entry-value>@ldap.url@</env-entry-value>
          </env-entry>
          <env-entry>
            <env-entry-name>username</env-entry-name>
            <env-entry-type>java.lang.String</env-entry-type>
            <env-entry-value>@user.name@</env-entry-value>
          </env-entry>
          <env-entry>
            <env-entry-name>password</env-entry-name>
            <env-entry-type>java.lang.String</env-entry-type>
            <env-entry-value>what</env-entry-value>
          </env-entry>
          <env-entry>
            <env-entry-name>com.suresh.ejb.test.HelloBean/location</env-entry-name>
            <env-entry-type>java.lang.String</env-entry-type>
            <env-entry-value>Australia</env-entry-value>
          </env-entry>
          <resource-ref>
            <res-ref-name>jdbc/user</res-ref-name>
            <res-type>javax.sql.Datasource</res-type>
            <res-auth>Container</res-auth>
            <mapped-name>java:UserDB</mapped-name>
          </resource-ref>
    
        </session>
      </enterprise-beans>
    
    </ejb-jar>
    

     

     

     

    package com.suresh.ejb.test;
    
    import java.sql.Connection;
    import java.sql.ResultSet;
    
    import javax.annotation.Resource;
    import javax.naming.Context;
    import javax.naming.InitialContext;
    import javax.sql.DataSource;
    
    import com.suresh.ejb.test.Hello;
    
    
    @javax.ejb.Stateless(name="com.suresh.ejb.test.HelloBean")
    @javax.ejb.TransactionAttribute (value=javax.ejb.TransactionAttributeType.SUPPORTS)
    @javax.annotation.Resource(name="jdbc/user",
      type=DataSource.class,
      authenticationType=javax.annotation.Resource.AuthenticationType.CONTAINER,
      mappedName="java:UserDB")
    public class HelloBean implements Hello {
    
      @Resource private String urls;
      @Resource(name="username") private String provider;
      @Resource(name="password") private String pwd;
      @Resource private String location;
    
      public String greet() {
        return urls + " " + location + " " + pwd + " " + provider + " at " + greet1();
      }
    
      // Example of a datasource.
      public String greet1() {
        String data = null;
        Connection c = null;
    
        try {
          Context ctx = new InitialContext();
          DataSource ds = (DataSource) ctx.lookup("java:comp.ejb3/env/jdbc/user");
         ResultSet rs = ds.getConnection().createStatement().executeQuery("select sysdate from dual");
         while (rs.next()) {
           data = rs.getString(1);
         }
        } catch (Exception e) {
           System.err.println(e);
           e.printStackTrace();
        } finally {
           try {
           c.close();
        } catch (Exception e) {
        }
      }
      return data;
      }
    }
    

     

     


    Hello.java

    package comsuresh.ejb.test;
    
    import javax.ejb.Remote;
    
    @Remote
    public interface Hello {
      public String greet();
    }
    

     

     

     

    This article was generated from the following discussion: EJB 3.0 and env-entry injection