SpringAop报错: Null return value from advice does not match primitive return type for

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 即可

还有个疑问,我的方法明明有返回值呀,怎么会是空呢??

先不说了, 继续研究去了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值