Java - Annotations

Java Conceptuel Diagram

About

Since Java version 1.5.

An annotation is an element of the java programming language (modifier or Metadata tag) that can be associated with:

A metadata annotation represents a Java language feature that lets you attach structured and typed metadata to the source code. Annotations alone are sufficient for the metadata specification–you do not need to use XML deployment descriptors

With Java EE annotations, the standard deployment descriptors (application.xml and web.xml) are optional.

Java EE 5 defines a number of annotations that can be injected into your source code. To declare an annotation, you simply precede the keyword with an “at” sign (@).

Annotations look like this:

@annotation-name ( parameters )

The call is similar to method calls except that they begin with @.

An annotation applies to a class or method and directly precedes the class or method definition.

They annotate the class or method with metadata, replacing configuration files.

Annotations replace boilerplate code, common code that is required by certain applications.

For example, an annotation can replace:

  • the paired interface and implementation required for a Web service.
  • additional files that programs require, which are maintained separately. By using an annotation, this separate file is no longer required.

For example, annotations can replace the need for a separately maintained deployment descriptor for JavaBeans™.

Annotations:

  • Replace descriptors for most purposes
  • Remove the need for marker interfaces (like java.rmi.Remote)
  • Allow application settings to be visible in the component they affect

Annotations have a number of uses, among them:

  • Information for the compiler — Annotations can be used by the compiler to detect errors or suppress warnings.
  • Compiler-time and deployment-time processing — Software tools can process annotation information to generate code, XML files, and so forth.
  • Runtime processing — Some annotations are available to be examined at runtime.

Advantages and Disadvantages

of Using Annotations

Metadata annotations are relatively simple to use and understand. They provide in-line metadata located with the code that this metadata is describing–you do not need to replicate the source code context of where the metadata applies.

On the other hand, annotations unnecessarily couple the metadata to the code. Thus, changes to metadata require changing the source code.

of Using XML

The following are advantages of using XML:

  • no coupling between the metadata and the source code;
  • compliance with the existing, pre-EJB 3.0 development process;
  • support in IDEs and source control systems;

The main disadvantages of mapping with XML are the complexity and the need for replication of the code context.

Task

Java EE 5 provides annotations for the following tasks, among others:

  • Developing Enterprise JavaBean applications
  • Defining and using Web services
  • Mapping Java technology classes to XML
  • Mapping Java technology classes to databases in JPA
  • Mapping methods to operations
  • Specifying external dependencies
  • Specifying deployment information, including security attributes

How to

Create an annotation

Suppose that a software group has traditionally begun the body of every class with comments providing important information:

public class Generation3List extends Generation2List {

   // Author: John Doe
   // Date: 3/17/2002
   // Current revision: 6
   // Last modified: 4/12/2004
   // By: Jane Doe
   // Reviewers: Alice, Bill, Cindy

   // class code goes here

}

To add this same metadata with an annotation, you must first define the annotation type. The syntax for doing this is:

@interface ClassPreamble {
   String author();
   String date();
   int currentRevision() default 1;
   String lastModified() default "N/A";
   String lastModifiedBy() default "N/A";
   String[] reviewers();  // Note use of array
}

Specification

Java™ EE 6 and Java EE 5 define a number of types or groups of annotations, defined in a number of Java Specification Requests (JSRs).

Java EE 5 defines the following types of annotations:

  • JSR 175: A Metadata Facility for the JavaTM Programming Language

Documentation / Reference





Discover More
Card Puncher Data Processing
Design Pattern - Dependency Injection

Dependency injection is: a dependency resolution mechanism where components (object, bean, client) are given their (type) dependencies (service, ..) (Dependencies are injected) and therefore are...
Card Puncher Data Processing
Eclipse - Project Facets

A Facet is an Eclipse WebTools Project concept which essentially means a capability attached to an Eclipse project. When a project is created, various information is assembled to: specify the type...
Java Conceptuel Diagram
J2EE - Component Registration

The component registration in a J2EE Application server is defined through: an annotation of a deployment descriptor
Jpa Mapping Method
JPA - Entity Annotations

A key feature of EJB 3.0 and JPA is the ability to create entities that contain object-relational mappings by using (metadata) annotations rather than deployment descriptors (orm.xml) as in earlier versions....
Card Puncher Data Processing
JPA - Entity Manager

To manage entities in your persistence application, you need to obtain an javax/persistence/EntityManagerentity manager from an javax/persistence/EntityManagerFactoryEntityManagerFactory. An javax/persistence/EntityManagerEntityManager...
Jdeveloper Create Managed Bean
JSF - (Managed Bean|Controller)

Managed Beans (also known as model objects and controllers) are lightweight container-managed objects (POJOs) with minimal requirements. They support a small set of basic services, such as: resource...
Java Conceptuel Diagram
Java - (Data Type|Type)

data type in the java world. The Java programming language is statically-typed, which means that all variables must first be declared before they can be used. The language supports four kinds...
Java Conceptuel Diagram
Java - Comments / Annotations

Comments and annotations are ignored by the compiler but are useful to other programmers. The Java programming language supports three kinds of comments. The compiler ignores everything from...
Java Conceptuel Diagram
Java - Dependency Injection (DI)

Dependency injection in Java. This page is injection of a java dependency in a class. Google: Dagger is is dependency injection at compile time while Guice is at runtime ...
Java Conceptuel Diagram
Java - Java Web Service (JWS) (Annotation Method)

Instead of coding the raw Java class or EJB directly, you use the JWS annotations programming model, which makes programming a WebLogic Web service much easier. This programing model takes advantage of...



Share this page:
Follow us:
Task Runner