Mastering Java Annotations: From Basics to Custom Usage and Framework Integration

This article explains what Java annotations are, why they were introduced, how they work, how to create and use custom annotations with reflection, and demonstrates their practical applications in popular frameworks and Oracle ADF.

Programmer DD
Programmer DD
Programmer DD
Mastering Java Annotations: From Basics to Custom Usage and Framework Integration

Annotations have been an important part of Java since J2SE 5.0. They appear frequently in code, such as @Override and @Deprecated. This article explores what annotations are, why they were introduced, how they function, how to write custom annotations (with example code), their valid use cases, and their relationship with Application Development Frameworks (ADF).

Grab a coffee and get ready to dive into the world of annotations.

1. What Is an Annotation?

In a word, an annotation is metadata —data about data. It provides metadata for code elements.

@Override
public String toString() {
    return "This is String Representation of current object.";
}

The @Override annotation tells the compiler that the method overrides a superclass method; if the method does not exist in the superclass, a compilation error is raised. Without the annotation the code still runs, but the safety check is lost.

2. Why Were Annotations Introduced?

Before annotations, XML was widely used for metadata, but maintaining XML became cumbersome. Developers wanted a way to tightly couple metadata with code, avoiding the loose coupling of XML. Annotations provide a standard way to define metadata directly in code.

For example, application‑wide constants could be stored in XML, while methods that should be exposed as services are better described with annotations.

3. How Annotations Work and How to Write Custom Annotations

Java provides four meta‑annotations in java.lang.annotation that are used when defining your own annotations: @Documented – whether the annotation appears in Javadoc. @Retention – when the annotation is retained (SOURCE, CLASS, RUNTIME). @Target – where the annotation can be applied (TYPE, FIELD, METHOD, etc.). @Inherited – whether the annotation is inherited by subclasses.

Example of a custom annotation:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Todo {
    String author() default "Yash";
    enum Priority {LOW, MEDIUM, HIGH}
    Priority priority() default Priority.LOW;
    enum Status {STARTED, NOT_STARTED}
    Status status() default Status.NOT_STARTED;
}

Using the custom annotation:

@Todo(priority = Todo.Priority.MEDIUM, author = "Yashwant", status = Todo.Status.STARTED)
public void incompleteMethod1() {
    // Some business logic
}

If an annotation has a single element, it can be named value and used without specifying the element name:

@interface Author {
    String value();
}

@Author("Yashwant")
public void someMethod() {}

To consume annotations at runtime, reflection is used. The getAnnotation() method returns the annotation instance, which can then be cast and its elements accessed.

Class<?> cls = BusinessLogic.class;
for (Method method : cls.getMethods()) {
    Todo todo = method.getAnnotation(Todo.class);
    if (todo != null) {
        System.out.println("Method: " + method.getName());
        System.out.println("Author: " + todo.author());
        System.out.println("Priority: " + todo.priority());
        System.out.println("Status: " + todo.status());
    }
}

4. Annotation Use Cases

Annotations are heavily used in frameworks such as Spring and Hibernate for logging, validation, and configuration. They replace marker interfaces and allow fine‑grained control over classes and methods.

Servlet 3.0 introduced several annotations for web components, e.g.: @WebServlet – declares a servlet. @WebFilter – declares a filter. @WebListener – declares a listener. @HandlesTypes, @HttpConstraint, @HttpMethodConstraint, @MultipartConfig – various security and configuration annotations.

5. Application Development Framework (ADF) and Annotations

Oracle ADF is a framework for building Fusion applications. While ADF does not use AOP like Spring or Hibernate, custom annotations can still be applied via inheritance or other mechanisms. The article discusses the limitations and possibilities of using annotations within ADF.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaReflectionframeworksannotationsCustom Annotations
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.