Java Annotation

The Annotations don’t come from Ejb but it’s undoubtedly the large use of it in Ejb (since version 3.0) inside the code.

Like hibernate and other framework, annotations are used to enrichment the code keeping separated the annotation from the core code. I like to think it as “Xml in code” because, sometimes, it really can substitute the xml configuration file.

You already know the typical jpa annotations that are used for mapping the real database table to the class.

In this example I would like to use the annotations for customizes some class behaviours.

The example is about processing order’s products . We’ll use annotations to check the vehicle type and shipping time for any products.

Let’s take a look at the code; first I defined the annotation interface:

package it.sample.orderannotation;

import java.lang.annotation.Retention;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

public @interface OrderAnnotation {

 public String vehicle();
 public String shippingTime();

We have three differents retention types:

RUNTIME, CLASS, SOURCE. Here the references for these (

We’ll use this class to annotated the two products class: Vegetables and Meat.

package it.sample.orderannotation;

@OrderAnnotation(shippingTime = "6h", vehicle = "ship")
public class Vegetables extends Product{

 public Vegetables(String description, String origin) {
  super(description, origin);


package it.sample.orderannotation;

@OrderAnnotation(shippingTime = "24h", vehicle = "railway")
public class Meat extends Product{

 public Meat(String description, String origin) {
  super(description, origin);  


All these class extend Product class

package it.sample.orderannotation;

public class Product {

 public String description;
 public String origin;
 public Product (String description, String origin)
  this.description = description;
  this.origin = origin;

At last, the Main class.

package it.sample.orderannotation;

import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.List;

public class Main {

  * @param args
 public static void main(String[] args) {
  List<Product> products = new ArrayList<Product>();
  products.add(new Vegetables("Apples", "Austria"));
  products.add(new Meat("Beef", "Ireland"));
  System.out.println("Check the order's products");
  for (Product product : products)
   System.out.println("Product:" + product.description);
   System.out.println("Origin:" + product.origin);
   Class<? extends Product> classAnnotated = product.getClass();
   Annotation annotation = classAnnotated.getAnnotations()[0];

      OrderAnnotation myAnnotation = (OrderAnnotation) annotation;
      System.out.println("vehicle: " + myAnnotation.vehicle());
      System.out.println("shippingTime: " + myAnnotation.shippingTime());



Get results:

Check the order's products
vehicle: ship
shippingTime: 6h
vehicle: railway
shippingTime: 24h

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.