问题场景
在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);
}
}
问题分析
解决
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;
}