- EJB 3.0 Database Persistence with Oracle Fusion Middleware 11g
- Deepak Vohra
- 510字
- 2021-04-13 17:23:07
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");
- 性能測試從零開始
- 中文版CorelDRAW X7基礎培訓教程(移動學習版)
- UI 設計入門一本就夠
- UG NX 9.0中文版 基礎教程 (UG工程師成才之路)
- Cinema 4D電商美工與視覺設計案例教程(培訓教材版)
- Web 2.0 Solutions with Oracle WebCenter 11g
- Android從入門到精通
- Mastering phpMyAdmin 3.1 for Effective MySQL Management
- 中文版Photoshop CS6應用技法教程
- Linux Shell Scripting Cookbook
- Refactoring with Microsoft Visual Studio 2010
- iPad Procreate風格繪畫之美
- Illustrator CC 2018 基礎與實戰教程(全彩版)
- Oracle E/Business Suite R12 Supply Chain Management
- 3ds Max三維動畫制作項目式教程