問題場景
在aop around方法裡傳回void,但是原方法傳回為int,
結果執行時抛異常
Null return value from advice does not match primitive return type for,
原方法如下
public int insert(LogRequestModel model) {
return logMapper.insert(model);
}
aop around方法如下
@Around("cut()")
public void around(ProceedingJoinPoint joinPoint){
long start = System.currentTimeMillis();
try {
joinPoint.proceed();
} catch (Throwable throwable) {
throw new GlobalException(throwable.getMessage());
}
long end = System.currentTimeMillis() - start;
if(end - start > 5000){
MethodSignature ms = (MethodSignature)joinPoint.getSignature();
log.info("類{}中的方法{}執行耗時{}", ms.getDeclaringTypeName(), ms.getMethod().getName(), end);
}
}
問題分析
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiAzNfRHLGZkRGZkRfJ3bs92YsYTMfVmepNHLwkkeOlXQE1UNNpHW4Z0MMBjVtJWd0ckW65UbM5WOHJWa5kHT20ESjBjUIF2X0hXZ0xCMx81dvRWYoNHLrdEZwZ1Rh5WNXp1bwNjW1ZUba9VZwlHdssmch1mclRXY39CXldWYtlWPzNXZj9mcw1ycz9WL49zZuBnLygzN4MDNxATM1IzNwAjMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
解決
around方法加上傳回值
@Around("cut()")
public Object around(ProceedingJoinPoint joinPoint){
long start = System.currentTimeMillis();
Object res = null;
try {
res = joinPoint.proceed();
} catch (Throwable throwable) {
throw new GlobalException(throwable.getMessage());
}
long end = System.currentTimeMillis() - start;
if(end - start > 5000){
MethodSignature ms = (MethodSignature)joinPoint.getSignature();
log.info("類{}中的方法{}執行耗時{}", ms.getDeclaringTypeName(), ms.getMethod().getName(), end);
}
return res;
}