@Around advice is the Spring AOP advice type that wraps a method call, so it can run code before and after the target and decide whether the target runs at all. That makes it the natural tool for measuring performance: you start a timer, call proceedingJoinPoint.proceed(), stop the timer and record the duration, without touching the business code.
The other advice types cannot do this on their own, because @Before and @After run in separate callbacks and cannot share a local timer variable.
An @Around method receives a ProceedingJoinPoint. Calling proceed() runs the intercepted method and returns its result. Code before the call runs first, code after it runs when the method returns or throws, and the advice can change arguments, swallow exceptions or replace the return value.
For timing this gives you one place with both timestamps, access to the method signature for labelling, and a finally block that records the duration even when the method fails.
Add the AOP starter. It brings Spring AOP and AspectJ's annotations and weaver.
In Spring Boot, AOP auto-proxying is on by default once the starter is present. In a plain Spring application add @EnableAspectJAutoProxy to a configuration class.
Notes on this code:
System.nanoTime() is the right clock for elapsed time because it is monotonic.throws Throwable and return Object, otherwise return values and exceptions would be lost.finally block records the time on success and failure.Timing every method in a package is noisy. A marker annotation lets you opt in.
Annotate service methods with @Timed to profile only those.
Logs are fine while developing. In production, publish timings as metrics so you can chart percentiles.
Spring's own @Timed from Micrometer already exists in io.micrometer.core.annotation. Spring Boot with Actuator can time methods through it when a TimedAspect bean is registered, so check that before writing your own.
this.otherMethod() inside the same bean bypasses the proxy, so that call is not timed.proceed() exactly once unless you intentionally retry or short-circuit.CompletableFuture or reactive types, the method returns before the work finishes, so the measured time is only the setup. Attach timing to the future or use reactive operators instead.Only @Around can hold a local variable such as a start time across the call and control execution. @Before and @After are separate callbacks.
Proxy interception adds a small cost per call. It is negligible for service-layer methods but avoid advising tiny methods in hot loops.
Usually the class is not a Spring bean, the method is called from within the same class, or the pointcut expression does not match.
Only when you need to advise non-Spring objects or self-invocation. For most services Spring's proxy-based AOP is enough.
@Around advice wraps the target method through ProceedingJoinPoint.proceed(), which makes it the right way to measure execution time in Spring. Use System.nanoTime() with a finally block, narrow the pointcut with a custom annotation, report metrics through Micrometer in production, and remember the limits around self-invocation and asynchronous return types.