在 Spring Boot 中優(yōu)雅的計算方法執(zhí)行時間
計算方法執(zhí)行時間是大多數(shù)后端應(yīng)用程序中常見的非功能性需求。以下是一些計算執(zhí)行時間的傳統(tǒng)方法:
long startTime = (System.currentTimeMillis());
// 你的邏輯代碼
long executionTime = (System.currentTimeMillis() - startTime) / 1000;
LOGGER.info("計算所花費的時間為:{} 秒", executionTime);
StopWatch watch = new StopWatch();
watch.start();
// 你的邏輯代碼
watch.stop();
LOGGER.info("計算所花費的時間為:{} 秒", watch.getTotalTimeSeconds());
可以看到需要在每個需要計算執(zhí)行時間的方法里增加相同的計算代碼,這種統(tǒng)計代碼執(zhí)行時間的方式存在代碼冗余、違反 DRY 原則、侵入性強等問題。
接下來讓我們通過 AOP 來實現(xiàn)在方法頂部添加一個注解,就可以非常高效地計算所有方法的執(zhí)行時間的功能。
步驟一:啟用 Spring AOP
首先,通過在 POM.xml 中添加以下內(nèi)容來啟用 Spring AOP:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
步驟二:創(chuàng)建自定義注解
現(xiàn)在讓我們在 Spring Boot 中創(chuàng)建一個名為 ExecutionTimeLogger 的自定義注解??梢钥吹?,該注解在運行時起作用,并且適用于方法級別。
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ExecutionTimeLogger {
}
步驟三:創(chuàng)建切面類
創(chuàng)建一個作為切面的新類,包含計算方法執(zhí)行時間的邏輯。在 Spring AOP 中有多種使用切面的方式,這里使用 @Around 注解。創(chuàng)建 ExecutionTimeLoggerAspect 類并使用 @Aspect 注解,再創(chuàng)建 executionTimeLogger 方法并添加 @Around 注解,將注解名稱作為參數(shù)傳遞給 @Around,這樣該方法會在帶 ExecutionTimeLogger 注解的方法執(zhí)行前后被調(diào)用:
@Aspect
@Component
publicclass ExecutionTimeLoggerAspect {
static final Logger logger = LoggerFactory.getLogger(ExecutionTimeLoggerAspect.class);
@Around("@annotation(ExecutionTimeLogger)")
public Object executionTimeLogger(ProceedingJoinPoint joinPoint) {
try {
long startTime = System.currentTimeMillis();
Object proceed = joinPoint.proceed();
long executionTime = (System.currentTimeMillis() - startTime);
logger.info("{}方法在{}毫秒內(nèi)執(zhí)行完畢", joinPoint.getSignature(), executionTime);
return proceed;
} catch (Throwable e) {
logger.error("在計算{}方法執(zhí)行時間時出錯", joinPoint.getSignature(), e);
return null;
}
}
}
步驟四:使用自定義注解
所有必需的配置都已完成?,F(xiàn)在我們唯一需要做的就是在想要計算 Java 代碼中方法執(zhí)行時間的地方使用該注解。假設(shè)要計算方法的執(zhí)行時間,如下面的代碼所示。只需在方法頂部添加注解即可。
public class CustomLoggingService {
@ExecutionTimeLogger
public void myMethod() {
try {
Thread.sleep(20000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
步驟五:查看輸出
當(dāng) myMethod 在你的代碼中執(zhí)行時的輸出:
c.n.t.a.ExecutionTimeLoggerAspect|void com.main.project.CustomLoggingService.myMethod()方法在20毫秒內(nèi)執(zhí)行完畢
是不是很簡單?想象一下節(jié)省了多少時間和精力。只需寫一遍計算執(zhí)行時間的代碼,然后在需要計算執(zhí)行時間的地方繼續(xù)使用注解即可。
總結(jié)一下:
- 創(chuàng)建自定義注解。
- 創(chuàng)建 @Aspect 類,包含計算執(zhí)行時間的方法。
- 用 @Around 聲明方法并指定為自定義注解執(zhí)行。
- 在要記錄執(zhí)行時間的方法上加注解。