Cagdas Basaraner

About Cagdas Basaraner

Cagdas Basaraner is a software engineer graduated from Hacettepe University Computer Engineering department (Turkey), having 5 years professional experience. He is working on JEE web technologies, and also a former developer of information systems using Microsoft technologies and Command & Control (C4I) systems with Java technologies.

Java Annotations & A Real World Spring Example

An “annotation” is a type of programming language definition and used as a “marker”. They can be thought as comment lines which programming language engine can understand. They don’t directly affect program execution but affect indirecly if wanted.

Definition

An annotation is defined with @interface keyword and is similar with an interface. It has attributes which are defined like interface methods. Attributes can have default values. Let’s define an annotation named “Page”, which defines UI pages of an application:

public @interface Page {
    int    id();
    String url();
    String icon() default "[none]";
    String name(); default "[none]";
}

Usage

Annotations are widely used to inform compiler or compile-time/runtime/deployment-time processing.
Usage of an annotation is simpler:

@Page(id=1, url=”studentView”, icon=“icons/student.png”, name=”Students”)
public class StudentWindow extends Window { … }

Annotations can also be defined for methods and attributes:

@AnAnnotation
public String getElementName() {…}

@AnAnnotation(type=”manager”, score=3)
public int income;

Examples

1) Reflection/code generation:

Methods having a specific annotation can be processed at runtime:

public @interface MyAnnotation { ... }
public class TestClass {
    @MyAnnotation
    public static method1() { ... }
    @MyAnnotation
    public static method2() { ... }
    @MyAnnotation
    public static method3() { ... }
}

public static void main(String[] args) {
    for (Method method : Class.forName("TestClass").getMethods()) {
        if (method.isAnnotationPresent(MyAnnotation.class)) {
            // do what you want
        }
    }
}

2) Spring bean configuration (this section requires Spring bean configuration knowledge):

Let’s use our “Page” annotation again:

package com.cmp.annotation;
public @interface Page {
    int    id();
    String url();
    String icon() default "[none]";
    String name(); default "[none]";
}

Say that we have a few classes having @Page annotation in a package:

@Page(id=1, url=”studentView”, icon=“icons/student.png”, name=”Students”)
public class StudentWindow extends Window { … }

If we define a bean configuration as below in a Spring application-context.xml file, Spring will create class instances “which has @Page annotation” placed in “given package”.

<context:component-scan base-package="com.cmp.ui" annotation-config="true">
<context:include-filter type="annotation" expression="com.cmp.annotation.Page"/>
</context:component-scan>

So, we have been enforced Spring to instantiate only a selection of classes at runtime.

For more detailed info about annotations, please refer to:

http://docs.oracle.com/javase/1.5.0/docs/guide/language/annotations.html
http://docs.oracle.com/javase/tutorial/java/javaOO/annotations.html

Reference: Java Annotations & A Real World Spring Example from our JCG partner Cagdas Basaraner at the CodeBalance blog.

Related Articles :

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

Leave a Reply


7 − four =



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close