下午遇到一个奇怪的问题
总结就一下就是
使用@Async 异步执行的方法,如果有返回值,返回值不要用原始数据类型(比如int),最好使用包装类(比如Integer)
因为使用原始类型的话,会报错
org.springframework.aop.AopInvocationException: Null return value from advice does not match primitive return type for: public abstract int cn.xxx.xxx.entry.service.IEntryServiceLogSrv.updateByRequestIdSelective(cn.xxx.xxx.entry.model.vo.http.QueryEntryInfoResponse)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:227) ~[spring-aop-4.3.19.RELEASE.jar:4.3.19.RELEASE]
at com.sun.proxy.$Proxy128.updateByRequestIdSelective(Unknown Source) ~[?:?]
在动态代理的源码中发现:
// file: JdkDynamicAopProxy.class -- spring-aop-4.3.19.RELEASE.jar
Class<?> returnType = method.getReturnType();
if (retVal != null && retVal == target && returnType != Object.class && returnType.isInstance(proxy) && !RawTargetAccess.class.isAssignableFrom(method.getDeclaringClass())) {
retVal = proxy;
} else if (retVal == null && returnType != Void.TYPE && returnType.isPrimitive()) {
throw new AopInvocationException("Null return value from advice does not match primitive return type for: " + method);
}
解决办法
- 改为无返回值 void
- 改为使用包装类
- 不使用异步注解