AOP in Micronaut: Introduction to @Around and Interceptors

Aspect-Oriented Programming (AOP) is a powerful way to separate cross-cutting concerns like logging, security, or transactions from your core business logic. In Micronaut, AOP is implemented using compile-time tools, making it lightweight and fast β€” perfect for microservices.

In this post, you’ll learn how AOP works in Micronaut, especially how to use the @Around annotation and create custom interceptors.

Micronaut AOP Tutorial

πŸ” What is AOP?

AOP helps you encapsulate behaviors that cut across multiple classes β€” like:

  • Logging
  • Security checks
  • Caching
  • Performance monitoring
  • Transaction management

These concerns can be injected at runtime without modifying the business logic itself.

πŸš€ Micronaut AOP with @Around

Micronaut supports AOP using @Around and interceptor bindings. These annotations allow you to wrap method execution logic using interceptors.

πŸ“¦ Maven Setup

Ensure your Maven project has the following dependencies in your pom.xml:



    
        io.micronaut
        micronaut-runtime
    
    
        io.micronaut
        micronaut-aop
    
    
        jakarta.inject
        jakarta.inject-api
    


πŸ› οΈ Create a Custom Annotation

We’ll start by defining a custom annotation to apply on methods.


package com.kscodes.micronaut.advanced;

import io.micronaut.aop.Around;
import java.lang.annotation.*;

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
@Around  // Micronaut-specific annotation
public @interface LogExecutionTime {
}

βš™οΈ Create the Interceptor

This class will handle logic around the method execution.


package com.kscodes.micronaut.advanced;

import io.micronaut.aop.MethodInterceptor;
import io.micronaut.aop.MethodInvocationContext;
import jakarta.inject.Singleton;

@Singleton
public class LogExecutionTimeInterceptor implements MethodInterceptor {

    @Override
    public Object intercept(MethodInvocationContext context) {
        long start = System.currentTimeMillis();
        try {
            return context.proceed(); // Execute the actual method
        } finally {
            long duration = System.currentTimeMillis() - start;
            System.out.println("Method [" + context.getMethodName() + "] executed in " + duration + "ms");
        }
    }
}

πŸ”„ Micronaut automatically wires this interceptor with your @LogExecutionTime annotation.

✨ Using the Custom Annotation


package com.kscodes.micronaut.advanced;

import jakarta.inject.Singleton;

@Singleton
public class ReportService {

    @LogExecutionTime
    public void generateReport() throws InterruptedException {
        Thread.sleep(500); // Simulate delay
        System.out.println("Report generated");
    }
}

πŸ§ͺ Test the AOP Logic

Create an entry point to call the service and observe the logs.


package com.kscodes.micronaut.advanced;

import io.micronaut.runtime.Micronaut;

public class Application {

    public static void main(String[] args) {
        var context = Micronaut.run(Application.class);
        ReportService reportService = context.getBean(ReportService.class);
        try {
            reportService.generateReport();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

πŸ” Real-World Use Cases for AOP in Micronaut

  • Logging user actions
  • Validating method arguments
  • Timing method execution (performance tracking)
  • Security permission checks
  • Auditing access to APIs

πŸ“ Tips for Working with AOP in Micronaut

  • Always use @Around in your custom annotations to enable interception.
  • Interceptors must be marked with @Singleton.
  • Use context.proceed() to ensure the actual method is executed.
  • Avoid overusing AOPβ€”it should enhance readability, not complicate it.

πŸ“˜ Conclusion

Micronaut AOP with @Around and custom interceptors gives you a powerful tool to cleanly inject behavior into your services. Unlike runtime AOP frameworks, Micronaut does this at compile time, keeping your apps fast and memory-efficient.

This Micronaut AOP tutorial helps you get started with real-world examples, showing how you can monitor and control method behavior without cluttering your business logic.