Mastering Java Annotations: From Basics to Custom AOP Integration

This article explains Java annotation fundamentals, explores built‑in meta‑annotations, demonstrates how to create compile‑time and runtime custom annotations, and shows how to integrate them with Spring Boot AOP for automatic logging and request handling.

Programmer DD
Programmer DD
Programmer DD
Mastering Java Annotations: From Basics to Custom AOP Integration

Preface

Annotations were introduced after JDK 1.5 in the package java.lang.annotation. They are special markers that can be read during compilation, class loading, or runtime to trigger processing. This article explains annotation basics, custom annotation creation, and how to combine them with AOP.

Annotation Analysis

How Annotations Work

Annotations such as @Override are markers checked by the compiler; they contain no logic themselves. The JVM treats them as metadata that can be processed by tools or frameworks.

Define annotation, scan annotation, execute logic.

Meta‑annotations

Java provides four meta‑annotations in java.lang.annotation: @Target – specifies where the annotation can be applied (e.g., TYPE, METHOD, FIELD, etc.). @Retention – defines the annotation’s lifecycle (SOURCE, CLASS, RUNTIME). @Documented – indicates that the annotation should appear in Javadoc. @Inherited – allows the annotation to be inherited by subclasses.

Custom Annotations

First, create a simple annotation interface, then implement a processor. The article shows a compile‑time annotation DataTest that prints “Hello World!” during compilation using an AbstractProcessor and Google’s auto‑service to register the processor.

<dependency>
    <groupId>com.google.auto.service</groupId>
    <artifactId>auto-service</artifactId>
    <version>1.0-rc5</version>
</dependency>

After adding the processor to the main project, annotating a class with @DataTest triggers the message at compile time.

Runtime Annotation Example

A runtime annotation @GetClassName is defined to capture a class name. An aspect intercepts methods annotated with @OutputLog to log request details, execution time, and results.

@Aspect
@Component
@Slf4j
public class LoggerAspect {
    @Pointcut("@annotation(com.lqcoder.annotationdemo.annotation.OutputLog)")
    public void weblog() {}

    @Around("weblog()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        long beginTime = System.currentTimeMillis();
        // log request, args, proceed, log duration, result
        return point.proceed();
    }
}

Integrating AOP

By adding a custom @Log annotation and the above aspect, the application can automatically log request URLs, parameters, execution time, and responses for any annotated endpoint.

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.

JavaaopRuntimeSpring BootannotationsCompile-time
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.