To create a custom aspect for performance monitoring in Spring, write an @Aspect component with an @Around advice that records the time before and after joinPoint.proceed() and logs or records the difference. Use System.nanoTime() for the measurement and a finally block so failing methods are timed too.
You can scope the aspect with a package pointcut, or, more precisely, with a custom @Timed-style annotation. For production systems, send the numbers to a metrics registry such as Micrometer instead of only writing log lines.
In Spring Boot, add the AOP starter. It pulls in spring-aop and AspectJ, and Boot auto-configures proxy creation, so @EnableAspectJAutoProxy is not needed.
In a plain Spring application without Boot, add @EnableAspectJAutoProxy to a @Configuration class instead. Logging comes with every Boot starter, so no separate logging dependency is required.
Points worth noting:
@Around is the only advice type that can run code before and after the target and control whether the target runs at all.proceed(), otherwise callers receive null.System.nanoTime() is monotonic, unlike System.currentTimeMillis(), which can jump when the clock is adjusted.finally block records the time even when the method throws, and the exception still propagates.toShortString() gives output such as UserService.getUserById(..) instead of the long fully qualified signature.The pointcut execution(* com.example.service..*(..)) matches every method in com.example.service and its sub-packages.
A package pointcut times everything, including trivial getters. A marker annotation lets you opt in method by method:
Now annotate only the methods you care about:
Logging every call is noisy. Log at INFO only above a threshold, and keep a DEBUG line for everything else:
Read the threshold from configuration with @Value("${app.perf.slow-ms:500}") so you can tune it per environment.
Log lines are hard to aggregate. With Spring Boot Actuator and Micrometer, record a timer and get percentiles and dashboards for free:
Micrometer also ships its own @Timed annotation and TimedAspect, which you can register as a bean instead of writing this yourself. Keep tag values low-cardinality: use the method name, never an id or user name.
| Limitation | Effect | Workaround |
|---|---|---|
| Proxy based | Self-invocation inside a class is not timed | Move the method to another bean |
| Spring beans only | Plain new objects are ignored | Register them as beans |
final classes or methods | Cannot be proxied with CGLIB | Remove final or use AspectJ weaving |
| Aspect overhead | Small cost per call | Scope the pointcut narrowly |
| Async methods | Measures only the hand-off | Time inside the async task |
For tracing across services, prefer OpenTelemetry or Micrometer Tracing over hand-written aspects.
The target must be a Spring bean called through its proxy from another bean, the pointcut must match its package, and spring-boot-starter-aop must be on the classpath. Self-invocation bypasses the proxy.
Use System.nanoTime() for elapsed time. It is monotonic, while currentTimeMillis() follows the wall clock and can be adjusted.
The overhead of one around advice is typically small compared with database or network calls, but avoid wide pointcuts on hot, tiny methods.
Yes. Micrometer's @Timed with TimedAspect, or Spring's StopWatch for ad hoc timing, cover many cases without a custom aspect.
A performance monitoring aspect wraps methods with @Around, measures with System.nanoTime() inside a try/finally, and returns the target's result. Scope it with a package pointcut or a marker annotation, alert only on slow calls, and publish timings to Micrometer for production-grade observability.