0
0
Spring Bootframework~5 mins

AOP for performance monitoring in Spring Boot

Choose your learning style9 modes available
Introduction

AOP helps measure how long parts of your program take to run without changing the main code. This makes it easy to find slow spots.

You want to check how fast a method runs in your app.
You need to log the time taken by database calls.
You want to monitor performance without adding timing code everywhere.
You want to find slow parts in your service methods.
You want to keep your main code clean and separate timing logic.
Syntax
Spring Boot
@Aspect
@Component
public class PerformanceAspect {

    @Around("execution(* com.example..*(..))")
    public Object measureTime(ProceedingJoinPoint joinPoint) throws Throwable {
        long start = System.currentTimeMillis();
        Object result = joinPoint.proceed();
        long end = System.currentTimeMillis();
        System.out.println(joinPoint.getSignature() + " took " + (end - start) + " ms");
        return result;
    }
}

@Aspect marks the class as an aspect for AOP.

@Around lets you run code before and after the method you watch.

Examples
This example measures time in nanoseconds and converts to milliseconds.
Spring Boot
@Around("execution(* com.example.service.*.*(..))")
public Object logTime(ProceedingJoinPoint joinPoint) throws Throwable {
    long start = System.nanoTime();
    Object result = joinPoint.proceed();
    long end = System.nanoTime();
    System.out.println(joinPoint.getSignature() + " took " + (end - start) / 1_000_000 + " ms");
    return result;
}
This example only measures methods marked with @TrackTime annotation.
Spring Boot
@Around("@annotation(com.example.TrackTime)")
public Object trackAnnotatedMethods(ProceedingJoinPoint joinPoint) throws Throwable {
    long start = System.currentTimeMillis();
    Object result = joinPoint.proceed();
    long end = System.currentTimeMillis();
    System.out.println("Method " + joinPoint.getSignature() + " took " + (end - start) + " ms");
    return result;
}
Sample Program

This aspect measures and prints how long any method in com.example.demo.service package takes to run.

Spring Boot
package com.example.demo;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class PerformanceAspect {

    @Around("execution(* com.example.demo.service.*.*(..))")
    public Object measureExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {
        long start = System.currentTimeMillis();
        Object proceed = joinPoint.proceed();
        long executionTime = System.currentTimeMillis() - start;
        System.out.println(joinPoint.getSignature() + " executed in " + executionTime + " ms");
        return proceed;
    }
}
OutputSuccess
Important Notes

Make sure to enable AspectJ auto proxy in your Spring Boot app with @EnableAspectJAutoProxy.

Use ProceedingJoinPoint.proceed() to run the original method.

Keep performance logging lightweight to avoid slowing down your app.

Summary

AOP lets you add timing code without changing your main methods.

Use @Around advice to measure method execution time.

This helps find slow parts and improve app speed easily.