Skip Headers
Oracle® Containers for J2EE Enterprise JavaBeans Developer's Guide
10g (10.1.3.5.0)

Part Number E13981-01
Go to Documentation Home
Home
Go to Book List
Book List
Go to Table of Contents
Contents
Go to Index
Index
Go to Feedback page
Contact Us

Go to previous page
Previous
Go to next page
Next
View PDF

Looking Up an EJB 3.0 Environment Variable

Using EJB 3.0, you can look up an environment variable using resource injection (see "Using Resource Injection") or the InitialContext (see "Using Initial Context").

Using Resource Injection

Using resource injection, you can rely on the container to initialize a field or a setter method (property) using either of the following:

  • default JNDI name (of the form java:comp/env/<FieldOrPropertyName>)

  • explicit JNDI name that you specify (do not prefix the name with "java:comp/env")

You cannot inject both field and setter using the same JNDI name.

The following examples show how to initialize the maxExemptions field with the value specified for the environment variable with the default JNDI name java:comp/env/maxExemptions.

You can use resource injection at the field level (see Example 19-26) or the setter method (property) level, as Example 19-27 shows.

Example 19-26 Resource Injection at Field Level with Default Environment Variable Name

@Stateless public class EmployeeServiceBean implements EmployeeService {
    ...
    // The maximum number of tax exemptions, configured by Deployer
    // Assumes JNDI name java:comp/env/maxExemptions.
    @Resource int maxExemptions;
    ...
    public void setMaxExemptions(int maxEx) {
        maxExemptions = maxEx;
    }
    ...
}

Example 19-27 Resource Injection at the Property Level with a Default Environment Variable Name

@Stateless public class EmployeeServiceBean implements EmployeeService {
    ...
    int maxExemptions;
    ...
    // Assumes JNDI name java:comp/env/maxExemptions.
    @Resource
    public void setMaxExemptions(int maxEx) {
        maxExemptions = maxEx;
    }
    ...
}

You can specify an explicit JNDI name, as Example 19-28 shows.

Example 19-28 Resource Injection with a Specific Environment Variable Name

@Stateless public class EmployeeServiceBean implements EmployeeService {
    ...
    int maxExemptions;
    ...
    @Resource(name="ApplicationDefaults/maxExemptions")
    public void setMaxExemptions(int maxEx) {
        maxExemptions = maxEx;
    }
    ...
}

Using Initial Context

Example 19-29 shows how you look up these environment variables within the bean's code using the InitialContext.

Example 19-29 Looking Up Environment Variables

InitialContext ic = new InitialContext();
Integer min = (Integer) ic.lookup("java:comp/env/minBalance");
Integer max = (Integer) ic.lookup("java:comp/env/maxCreditBalance"));

Notice that to retrieve the values of the environment variables, you prefix each environment element with "java:comp/env/", which is the location that the container stored the environment variable.

For more information, see "Configuring the Initial Context Factory".