EJB 3.0 Database Persistence with Oracle Fusion Middleware 11g
上QQ阅读APP看书,第一时间看更新

Environmental dependencies and JNDI Access

An enterprise bean's context may be divided into 3 components:

  • Container context
  • Resources
  • Environment context

The container may be used to supply references to resources and environment entries. Environmental dependencies and JNDI access may be encapsulated with dependency annotations, a dependency injection mechanism, and a simple lookup mechanism. Dependency injection implies that the EJB container automatically supplies/injects a bean's variable or setter method with a reference to a resource or environment entry in the bean's context. Alternatively, you would have to use the javax.ejb.EJBContext or JNDI APIs to access the environment entries and resources. Dependency injection is implemented by annotating a bean's variable or setter method with one of the following annotations:

  • @javax.ejb.EJB is used to specify dependency on another EJB.
  • @javax.annotation.Resource is used to specify dependency on an external resource such as a JDBC datasource, a JMS destination, or a JMS connection factory. The @Resource annotation is not specific to EJB 3, and may be also used with other Java EE components.

For accessing multiple resources, use the corresponding grouping annotations @javax.ejb.EJBs and @javax.annotation.Resources. An example of injecting dependency on an EJB into a bean's variable using the @javax.ejb.EJB annotation is as follows:

import javax.ejb.EJB;
@Stateful
public class CatalogBean implements Catalog {
@EJB(beanName = "HelloBean")
private Hello hello;
public void helloFromCatalogBean() {
hello.hello();
}
}

In the preceding example, the hello variable is injected with the EJB HelloBean. The type of the hello variable is Hello, which is the HelloBean's business interface that it implements. Subsequently, we invoked the hello() method of the HelloBean. A resource may also be injected into a setter method. If the resource type can be determined from the parameter type, the resource type is not required to be specified in the @Resource annotation. In the following code snippet, the setter method is annotated with the @Resource annotation. In the setter method, the dataSource property is set to a JNDI resource of type javax.sql.DataSource with value as catalogDB.

private javax.sql.DataSource dataSource;
@Resource(name="catalogDB")
public void setDataSource (DataSource jndiResource) {
this.dataSource = jndiResource;
}

The setter method must follow the JavaBean conventions: the method name begins with set, returns void, and has only one parameter. If the name of the resource is the same as the property name, the resource name is not required to be specified in the @Resource annotation. The JNDI name of the resource is of the format class_name/catalogDB, class_name being the class name.

private javax.sql.DataSource catalogDB;
@Resource
public void setCatalogDB (DataSource jndiResource) {
this.catalogDB = jndiResource;
}

Setter injection methods are invoked by the container before any business methods on the bean instance. Multiple resources may be injected using the @Resources annotation. For example, in the following code snippet two resources of type javax.sql.DataSource are injected.

@Resources({
@Resource(name="ds1", type="javax.sql.DataSource"),
@Resource(name="ds2", type="javax.sql.DataSource")
})

JNDI resources injected with the dependency mechanism may be looked up in the java:comp/env namespace. For example, if the JNDI name of a resource of type javax.sql.DataSource is catalogDB, the resource may be looked up as follows.

InitialContext ctx = new InitialContext();
Javax.sql.DataSource ds = ctx.lookup("java:comp/env/catalogDB");