Master SpringBoot Performance Monitoring with Built‑in Interceptors and Custom AOP
This guide explains SpringBoot's built‑in performance monitoring interceptors, demonstrates how to configure them with custom annotations, pointcuts, and advisors, and shows how to extend SimpleTraceInterceptor and CustomizableTraceInterceptor for detailed method‑execution logging and timing.
1. Introduction
SpringBoot 3.2.5 provides three ready‑made performance‑monitoring interceptors that share the common base class AbstractTraceInterceptor :
PerformanceMonitorInterceptor – records method execution time.
SimpleTraceInterceptor – tracks method entry, exit and exceptions.
CustomizableTraceInterceptor – combines timing and tracing with customizable log messages.
These interceptors can be used directly or extended by inheriting AbstractTraceInterceptor to implement custom monitoring logic.
2. Practical Examples
2.1 PerformanceMonitorInterceptor
The interceptor uses a StopWatch to mark the start and end of a method execution.
Custom annotation
<code>@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Monitor {}
</code>Methods annotated with @Monitor will be intercepted.
Custom pointcut
<code>@Pointcut("@annotation(com.pack.PerformanceMonitorConfig.Monitor)")
public void monitor() {}
</code>Advisor bean
<code>@Bean
public Advisor performanceMonitorAdvisor() {
DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor();
advisor.setAdvice(new PerformanceMonitorInterceptor(true));
AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
pointcut.setExpression("com.pack.PerformanceMonitorConfig.monitor()");
advisor.setPointcut(pointcut);
return advisor;
}
</code>After defining the advisor, a test endpoint can be created:
<code>@Monitor
@GetMapping("/index")
public Object index() throws Exception {
int sleep = new Random().nextInt(2000);
// simulate delay
TimeUnit.MILLISECONDS.sleep(sleep);
return String.format("业务处理耗时: %d", sleep);
}
</code>Calling this endpoint prints the execution time in the console (see screenshot).
2.2 SimpleTraceInterceptor
This interceptor logs method entry, exit and exception information. Its configuration is identical to the previous interceptor; only the log output differs (see screenshots).
2.3 CustomizableTraceInterceptor
By using CustomizableTraceInterceptor you can define your own log messages for three events:
setEnterMessage – when a method is entered.
setExitMessage – when a method exits.
setExceptionMessage – when an exception occurs.
Place‑holders such as $[methodName] , $[targetClassName] , $[invocationTime] , etc., can be used in the messages (some placeholders are not allowed for certain messages, e.g., $[returnValue] cannot be used in setEnterMessage ).
Example bean configuration:
<code>@Bean
public CustomizableTraceInterceptor customizableTraceInterceptor() {
CustomizableTraceInterceptor interceptor = new CustomizableTraceInterceptor();
interceptor.setExitMessage("Exiting method '" +
CustomizableTraceInterceptor.PLACEHOLDER_METHOD_NAME +
"' of class [" + CustomizableTraceInterceptor.PLACEHOLDER_TARGET_CLASS_NAME +
"], execution time: 【" + CustomizableTraceInterceptor.PLACEHOLDER_INVOCATION_TIME + "】ms");
interceptor.setUseDynamicLogger(true);
return interceptor;
}
</code>The resulting log shows method name, class and execution time (see screenshot).
3. Extending the Base Interceptor
If the default log format is unsatisfactory, you can override the protected method invokeUnderTrace in a subclass of AbstractTraceInterceptor :
<code>protected Object invokeUnderTrace(MethodInvocation invocation, Log logger) throws Throwable {
String name = createInvocationTraceName(invocation);
StopWatch stopWatch = new StopWatch(name);
stopWatch.start(name);
try {
return invocation.proceed();
} finally {
stopWatch.stop();
writeToLog(logger, stopWatch.shortSummary());
}
}
</code>This method is designed for developers to provide custom logging behavior.
All the code snippets above, together with the screenshots, constitute a complete guide to using SpringBoot's performance‑monitoring interceptors.
Spring Full-Stack Practical Cases
Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.