0
0
Spring Bootframework~7 mins

@Around advice for full control in Spring Boot

Choose your learning style9 modes available
Introduction

@Around advice lets you wrap extra code before and after a method runs. It gives you full control to change inputs, outputs, or handle errors.

You want to log method start and end times with details.
You need to measure how long a method takes to run.
You want to modify the method's input parameters or output result.
You want to handle exceptions in a custom way around a method.
You want to add security checks before a method runs.
Syntax
Spring Boot
@Around("pointcutExpression")
public Object methodName(ProceedingJoinPoint joinPoint) throws Throwable {
    // code before method
    Object result = joinPoint.proceed();
    // code after method
    return result;
}

The method must return Object and throw Throwable.

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

Examples
This example logs how long any method in com.example.service package takes to run.
Spring Boot
@Around("execution(* com.example.service.*.*(..))")
public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {
    long start = System.currentTimeMillis();
    Object proceed = joinPoint.proceed();
    long duration = System.currentTimeMillis() - start;
    System.out.println(joinPoint.getSignature() + " took " + duration + " ms");
    return proceed;
}
This example changes the first argument to uppercase before calling the original method.
Spring Boot
@Around("execution(* com.example.service.UserService.getUser(..))")
public Object modifyInput(ProceedingJoinPoint joinPoint) throws Throwable {
    Object[] args = joinPoint.getArgs();
    if(args.length > 0 && args[0] instanceof String) {
        args[0] = ((String) args[0]).toUpperCase();
    }
    return joinPoint.proceed(args);
}
Sample Program

This Spring Boot example uses @Around advice to measure how long any method in the service package takes to run. When the greet method runs, the aspect prints the execution time. The main app calls greet and prints the greeting.

Spring Boot
package com.example.demo.aspect;

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 TimingAspect {

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

// Service class
package com.example.demo.service;

import org.springframework.stereotype.Service;

@Service
public class GreetingService {
    public String greet(String name) {
        return "Hello, " + name + "!";
    }
}

// Main application to test
package com.example.demo;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.example.demo.service.GreetingService;

@SpringBootApplication
public class DemoApplication implements CommandLineRunner {

    private final GreetingService greetingService;

    public DemoApplication(GreetingService greetingService) {
        this.greetingService = greetingService;
    }

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        String message = greetingService.greet("Alice");
        System.out.println(message);
    }
}
OutputSuccess
Important Notes

Always call joinPoint.proceed() to continue the original method execution.

You can modify arguments by passing a new array to proceed(args).

Use @Around advice carefully as it can change method behavior and affect performance.

Summary

@Around advice wraps a method to run code before and after it.

It gives full control to modify inputs, outputs, or handle exceptions.

Remember to call joinPoint.proceed() to run the original method.