SpringAop报错: Null return value from advice does not match primitive return type for
今天哼哧哼哧码了一下午的代码,终于搞定,以为大功告成,一调接口发现:
经过查看发现,是由于调用的方法添加了 @Async 注解
这个注解的作用是通过Cglib创建代理类,然后异步执行方法。 来看看
class CglibAopProxy implements AopProxy, Serializable {
/**
* Process a return value. Wraps a return of {@code this} if necessary to be the
* {@code proxy} and also verifies that {@code null} is not returned as a primitive.
处理返回值, 包装当前代理类的返回 并且验证返回的不是 null
*/
@Nullable
private static Object processReturnType(
Object proxy, @Nullable Object target, Method method, @Nullable Object returnValue) {
// Massage return value if necessary
if (returnValue != null && returnValue == target &&
!RawTargetAccess.class.isAssignableFrom(method.getDeclaringClass())) {
// Special case: it returned "this". Note that we can't help
// if the target sets a reference to itself in another returned object.
returnValue = proxy;
}
Class<?> returnType = method.getReturnType();
//这里就是我们报错的地方
if (returnValue == null && returnType != Void.TYPE && returnType.isPrimitive()) {
throw new AopInvocationException(
"Null return value from advice does not match primitive return type for: " + method);
}
return returnValue;
}
}
当方法返回值是null 并且 不为Void类,isPrimitive 此方法主要用来判断Class是否为原始类型(boolean、char、byte、short、int、long、float、double)
返回的null, 然后自己用 boolean接收了, 基本类型不能被赋值为null,所以报错。
将当前方法的返回 由基本类型改为包装类型解决 boolean----> Boolean 即可
还有个疑问,我的方法明明有返回值呀,怎么会是空呢??
先不说了, 继续研究去了