EJB 3.0 uses annotations extensively. Everything is declared, expressed, and configured using annotations. Annotations are a special type of modifier in the form of metadata. Annotations precede other modifiers and are represented by an @ sign followed by a set of name-value pairs elements in parentheses. They do not affect program semantics directly, but they do affect how libraries and tools treat program code.
Annotations employ declarative programming. Instead of directly specifying a set of rules for the container to execute, annotations supply a set of conditions. The container resolves these conditions and decides how to proceed. To access resources in EJB 2.x, you specify dependencies in a deployment descriptor and obtain a reference to these resources using a JNDI lookup. With dependency injection, component dependencies are automatically injected in the component by the container using annotations. The container also manages the life cycle of injected resources. Dependency injection can be applied on Enterprise JavaBeans and Interceptor classes. You do not need to look up resources explicitly. If the name of the resource to be injected is the same as the variable you use, you can omit the name attribute or the @Resource annotation.
...
1. To inject a resource in a bean, you use the @Resource annotation.
// injects a data source object @Resource(name="jdbc/__default", type="DataSource.class") DataSource dataSource; |
The type attribute of the @Resource annotation is optional because it defaults to the data type of the resource variable to be injected.
You can also use the@Resource annotation to obtain references to resources such as javax.transaction.UserTransaction, javax.jms.Queue, javax.ejb.SessionContext, org.omg.CORBA.ORB, and so on.
2. To obtain a reference to the business interface or home interface of another bean, you use the @EJBannotation:
@EJB HRServices hrservices ; |
Annotation Reference Table
Name |
Use |
Target |
Annotation Attribute |
@EJB |
Use it to denote a reference to an EJB business interface. |
TYPE METHOD FIELD |
name - refers to the name by which the resource is to be looked up in the environment. The default value is ””.
beanInterface – the referenced interface type. The default value is Object.class.
beanName - the value of the name member of the reference bean’s @Stateful or @Stateless annotation. The default value is ””.
mappedName - you map the bean reference to this name. Applications that use mapped names are not portable. The default value is ””. |
@EJBs |
Use it to declare references to EJB business interfaces. |
TYPE |
value |
@Resource |
Use it to specify a dependency on a resource in the bean’s environment. |
TYPE METHOD FIELD |
name - the name by which the resource is known in the environment. The default value is ””.
type – the resource manager connection factory type. The default value is Object.class.
authenticationType - specifies whether the container or bean is to perform authentication. The default value is CONTAINER.
shareable - the default value is true.
mappedName - you map the bean reference to this name. Applications that you mapped names are not portable. The default value is ””.
description - the default value is ””. |
@Resources |
Acts as a container for multiple resource declarations because the repeated annotations are not allowed. |
TYPE |
value |