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

還有個疑問,我的方法明明有返回值呀,怎麼會是空呢??

先不說了, 繼續研究去了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章