7
Using Java EE Annotations and Dependency Injection 7-1
7
Using Java EE Annotations and Dependency Injection
The following sections describe the concepts of MetaData annotation and dependency injection:
■
Section 7.1, Annotation Processing
■
Section 7.2, Dependency Injection of Resources
■
Section 7.3, Standard JDK Annotations
■
Section 7.4, Standard Security-Related JDK Annotations
7.1 Annotation Processing
With Java EE annotations, the standard application.xml and web.xml deployment descriptors are optional. The Java EE programming model uses the JDK
5.0 annotations feature for Web containers, such as EJBs, servlets, Web applications, and JSPs see
http:java.sun.comjavaee5docsapi .
Annotations simplify the application development process by allowing developers to specify within the Java class itself how the application component behaves in the
container, requests for dependency injection, and so on. Annotations are an alternative to deployment descriptors that were required by older versions of Enterprise
applications J2EE 1.4 and earlier.
7.1.1 Annotation Parsing
The application components can use annotations to define their needs. Annotations reduce or eliminate the need to deal with deployment descriptors. Annotations
simplify the development of application components. The deployment descriptor can still override values defined in the annotation. One usage of annotations is to define
fields or methods that need Dependency Injection DI. Annotations are defined on the POJO plain old Java object component classes like the EJB or the servlet.
An annotation on a field or a method can declare that fieldsmethods need injection, as described in
Section 7.2, Dependency Injection of Resources . Annotations may
also be applied to the class itself. The class-level annotations declare an entry in the application components environment but do not cause the resource to be injected.
Instead, the application component is expected to use JNDI or component context lookup method to lookup the entry. When the annotation is applied to the class, the
JNDI name and the environment entry type must be specified explicitly.
7-2 Developing Applications for Oracle WebLogic Server
7.1.2 Deployment View of Annotation Configuration
The Java EE Deployment API [JSR88] provides a way for developers to examine deployment descriptors. For example, consider an EJB Module that has no deployment
descriptors. Assuming that it has some classes that have been declared as EJBs using annotations, a user of Session Helper will still be able to deal with the module as if it
had the deployment descriptor. So the developer can modify the configuration information and it will be written out in a deployment plan. During deployment, such
a plan will be honored and will override information from annotations.
7.1.3 Compiling Annotated Classes
The WebLogic Server utility appc and its Ant equivalent wlappc and Appmerge support metadata annotations. The appmerge and appc utilities take an application
or module as inputs and process them to produce an output application or module respectively. When used with -writeInferredDescriptors flag, the output
applicationmodule will contain deployment descriptors with annotation information. The descriptors will also have the metadata-complete attribute set to true, as no
annotation processing needs to be done if the output application or module is deployed directly. However, setting of metadata-complete attribute to true will
also restrict appmerge and appc from processing annotations in case these tools are invoked on a previously processed application or module.
The original descriptors must be preserved in such cases to with an .orig suffix. If a developer wants to reapply annotation processing on the output application, they
must restore the descriptors and use the -writeInferredDescriptors flag again. If appmerge or appc is used with -writeInferredDescriptors on an Enterprise
application for which no standard deployment descriptor exists, the descriptor will be generated and written out based on the inference rules in the Java EE specification.
For more information on using appc, see Section 4.2.4, weblogic.appc Reference
. For more information on using appmerge, see
Section 9.5, Using weblogic.appmerge to Merge Libraries
.
7.1.4 Dynamic Annotation Updates