
本文探讨了在spring boot应用中,如何在方法执行过程中,即使发生异常并由全局异常处理器捕获时,也能准确记录方法执行时间。文章提出了两种主要解决方案:利用spring aop实现横切关注点,在切面中统一测量时间并处理异常;或者通过自定义异常类,在其中封装执行时间信息,供异常处理器获取。这两种方法都能帮助开发者实现更完善的性能监控和异常日志记录。
在Spring Boot应用程序开发中,记录方法的执行时间是性能监控和问题诊断的关键环节。然而,当业务逻辑中出现异常,并且这些异常被全局的@ExceptionHandler方法统一处理时,如何在异常处理流程中获取到该方法的完整执行时间,成为了一个常见挑战。传统的做法是在每个方法内部使用try-catch块来计算时间,但这会导致大量重复代码,降低代码可维护性。本文将介绍两种更优雅、更具通用性的解决方案。
Spring AOP(面向切面编程)是解决这类横切关注点(如日志记录、性能监控、事务管理等)的理想方案。通过AOP,我们可以在不修改原有业务逻辑代码的情况下,在方法执行前后织入计时和异常处理逻辑。
首先,确保你的pom.xml中包含了Spring AOP的Starter依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>接下来,创建一个切面(Aspect)来定义计时逻辑。我们使用@Around通知,因为它允许我们在目标方法执行前后执行自定义逻辑,并且可以捕获目标方法抛出的异常。
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.time.Duration;
import java.time.Instant;
@Aspect
@Component
public class PerformanceMonitorAspect {
private static final Logger log = LoggerFactory.getLogger(PerformanceMonitorAspect.class);
// 定义切点,这里以所有Service层方法为例
// 你可以根据需要调整切点表达式,例如特定注解、特定包下的所有方法等
@Around("execution(* com.example.app.service.*.*(..))")
public Object measureExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {
Instant start = Instant.now();
Object result;
String methodName = joinPoint.getSignature().toShortString();
try {
result = joinPoint.proceed(); // 执行目标方法
} catch (Throwable e) {
Instant end = Instant.now();
long executionTime = Duration.between(start, end).toMillis();
log.error("方法 {} 执行异常,耗时 {} ms. 异常信息: {}", methodName, executionTime, e.getMessage(), e);
throw e; // 重新抛出异常,以便ExceptionHandler能够捕获
}
Instant end = Instant.now();
long executionTime = Duration.between(start, end).toMillis();
log.info("方法 {} 正常执行完成,耗时 {} ms", methodName, executionTime);
return result;
}
}代码解释:
有了上述切面,你的全局@ExceptionHandler将继续正常工作,因为它会捕获到由切面重新抛出的异常。
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
@ControllerAdvice
public class GlobalExceptionHandler {
private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
@ExceptionHandler(Exception.class)
public ResponseEntity<String> handleAllExceptions(Exception e) {
// 这里的e就是由PerformanceMonitorAspect重新抛出的原始异常
// 在切面中已经记录了执行时间,这里可以专注于异常响应的构建
log.error("全局异常处理器捕获到异常: {}", e.getMessage(), e);
return new ResponseEntity<>("服务器内部错误:" + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
}
}优点:
如果因为某些特定原因不希望使用AOP,或者需要将执行时间信息直接传递给ExceptionHandler,可以考虑自定义一个异常类来封装执行时间。
创建一个继承自RuntimeException的自定义异常类,并添加一个字段来存储执行时间。
import java.time.Duration;
public class TimeMeasuredException extends RuntimeException {
private final Duration executionDuration;
private final Throwable originalCause; // 存储原始异常
public TimeMeasuredException(Duration executionDuration, Throwable originalCause) {
super("方法执行异常,耗时 " + executionDuration.toMillis() + " ms", originalCause);
this.executionDuration = executionDuration;
this.originalCause = originalCause;
}
public Duration getExecutionDuration() {
return executionDuration;
}
public Throwable getOriginalCause() {
return originalCause;
}
}在需要测量时间的方法内部,使用try-catch块来捕获异常,计算时间,然后抛出TimeMeasuredException。
import java.time.Duration;
import java.time.Instant;
public class MyService {
public void doSomeFancyStuff() {
Instant start = Instant.now();
try {
// ... 你的业务逻辑代码 ...
if (Math.random() > 0.5) {
throw new RuntimeException("模拟业务异常");
}
// ...
} catch (Exception e) {
Instant end = Instant.now();
Duration executionTime = Duration.between(start, end);
// 捕获到原始异常后,计算时间并抛出自定义异常
throw new TimeMeasuredException(executionTime, e);
}
}
}注意: 这种方法要求在每个可能抛出异常并需要记录时间的地方都进行这样的try-catch封装。如果方法数量很多,这仍然会引入重复代码。更推荐的做法是,在AOP切面内部执行此逻辑,而不是在每个业务方法中。
示例:在AOP中抛出TimeMeasuredException 如果你想结合AOP的非侵入性,但又想利用自定义异常传递时间,可以这样做:
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import java.time.Duration;
import java.time.Instant;
@Aspect
@Component
public class TimeMeasuringAspectWithCustomException {
@Around("execution(* com.example.app.service.*.*(..))")
public Object measureAndWrapException(ProceedingJoinPoint joinPoint) throws Throwable {
Instant start = Instant.now();
Object result;
try {
result = joinPoint.proceed(); // 执行目标方法
} catch (Throwable e) {
Instant end = Instant.now();
Duration executionTime = Duration.between(start, end);
// 捕获到原始异常后,计算时间并抛出自定义异常
throw new TimeMeasuredException(executionTime, e);
}
return result;
}
}全局异常处理器现在可以专门捕获TimeMeasuredException,从而获取执行时间信息。
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
@ControllerAdvice
public class GlobalExceptionHandler {
private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
@ExceptionHandler(TimeMeasuredException.class)
public ResponseEntity<String> handleTimeMeasuredException(TimeMeasuredException e) {
log.error("捕获到带有执行时间的异常,耗时 {} ms. 原始异常信息: {}",
e.getExecutionDuration().toMillis(), e.getOriginalCause().getMessage(), e.getOriginalCause());
return new ResponseEntity<>("业务处理失败,耗时 " + e.getExecutionDuration().toMillis() + " ms. 错误详情: " + e.getOriginalCause().getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
}
// 仍然可以保留一个通用的异常处理器来捕获其他未被TimeMeasuredException包装的异常
@ExceptionHandler(Exception.class)
public ResponseEntity<String> handleAllOtherExceptions(Exception e) {
log.error("捕获到其他未处理的异常: {}", e.getMessage(), e);
return new ResponseEntity<>("服务器内部错误:" + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
}
}优点:
缺点:
在Spring Boot中记录方法执行时间并结合异常处理,最佳实践是采用Spring AOP。它能够以非侵入的方式,在应用层面统一管理性能监控和异常日志,极大地提高了代码的整洁度和可维护性。通过在@Around通知中捕获异常并重新抛出,可以确保全局@ExceptionHandler仍然能够正常工作,同时在切面中完成计时和日志记录。
自定义异常类的方式虽然也能达到目的,但更适合于需要将特定业务上下文(而不仅仅是执行时间)与异常一同传递的场景。如果仅仅是为了记录执行时间,AOP是更优的选择。
无论选择哪种方案,关键都在于将计时逻辑从业务方法中分离出来,使其成为一个独立的、可复用的组件,从而构建出更健壮、更易于维护的Spring Boot应用程序。
以上就是Spring Boot中优雅地记录方法执行时间并结合异常处理的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号