【Spring】- Spring 返回通知报错 Null return value from advice does not match primitive return type for ⭐️⭐️

本文主要是介绍【Spring】- Spring 返回通知报错 Null return value from advice does not match primitive return type for ⭐️⭐️,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

报错信息

Exception in thread "main" org.springframework.aop.AopInvocationException: Null return value from advice does not match primitive return type for: public abstract int com.wangt.spring.aspactj.annotation.Calculator.add(int,int)at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:226)at com.sun.proxy.$Proxy15.add(Unknown Source)at com.wangt.spring.aspactj.annotation.Main.main(Main.java:19)

代码

@Component() // 添加当前类到spring容器中
public class CalculatorImpl implements Calculator {@Overridepublic int add(int i, int j) {System.out.println("The method add be execute");int result = i + j;return result;}@Overridepublic int sub(int i, int j) {System.out.println("The method sub be execute");int result = i - j;return result;}@Overridepublic int mul(int i, int j) {System.out.println("The method mul be execute");int result = i * j;return result;}@Overridepublic int div(int i, int j) {System.out.println("The method div be execute");int result = i / j;return result;}
}
@Component //标示当前类为 spring的一个 组件
@Aspect // 声明当前类为切面类
@Order(2)
public class LoggingAspect {/*** 前置通知 : 在目标方法(连接点)之前执行* 切入点表达式 : execution(修饰符 返回值 包名.类名.方法名(参数类型1,参数类型2))*/@Before("execution(public int com.wangt.spring.aspactj.annotation.CalculatorImpl.add(..))")public void beforeMethod(JoinPoint point) {// 获取传入当前连接点的参数的值Object[] args = point.getArgs();String kind = point.getKind();System.out.println("LoggingAspect ==> The method " + kind + " begin with " + Arrays.asList(args));}/*** 后置通知 在目标方法(连接点)之后执行* 无论连接点是否正常返回结果 或者是抛出异常, 后置通知都会执行* 表达式 :* 第一个 * 表示 任意的返回类型* 第二个 .. 表示 第一个参数类型为 int , 其他的参数个数以及类型任意* JoinPoint : 当前连接点所在方法的方法名、当前传入的参数值等等。这些信息都封装在 JoinPoint 对象汇总*/@After("execution(public * com.wangt.spring.aspactj.annotation.CalculatorImpl.add(int ,..) )")public void afterMethod(JoinPoint point) {// 获取传入连接点的参数的值Object[] args = point.getArgs();System.out.println("LoggingAspect ==> The method xxx end with " + Arrays.asList(args));}/*** 返回通知 : 当连接点正常返回数据的时候会执行返回通知, 如果执行方法的时候出现异常,* 则不会 执行返回通知* 如果需要获取连接点返回的值,则需要在注解中添加 returning 参数的值* 并且需要在返回方法的参数列表中添加 和 returning 参数的值 一样的参数的值 类型为 Object** @param point* @param result*/@AfterReturning(value = "execution(* com.wangt.spring.aspactj.annotation.CalculatorImpl.*(..))",returning = "result")public void afterReturing(JoinPoint point, Object result) {Object[] args = point.getArgs();System.out.println("LoggingAspect ==> The method "+Arrays.asList(args)+ " result : " + result);}/*** 异常通知:只在连接点抛出异常时才执行异常通知* (1) throwing 属性添加到@AfterThrowing 注解中,也可以访问连接点抛出的异常。* (2) Throwable 是所有错误和异常类的顶级父类,所以在异常通知方法可以捕获到任何错误* 和异常。* 3) 如果只对某种特殊的异常类型感兴趣,可以将参数* @param point* @param exception*/@AfterThrowing(value = "execution(* com.wangt.spring.aspactj.annotation.CalculatorImpl.div(..))",throwing = "exception")public void afterThrowMethod(JoinPoint point , Exception exception){// 获取方法的名字String methodName = point.getSignature().getName();System.out.println("LoggingAspect ==> The method "+ methodName + "occurs Exception " + exception);}/*** 环绕通知可以直接设置 前置 后置 返回 异常通知 , 并且执行的优先级要大于前面的* @param point ProceedingJoinPoint 的对象 , 是JoinPoint 的子类*/@Around("execution(* com.wangt.spring.aspactj.annotation.CalculatorImpl.*(..))")public void aroundMethod(ProceedingJoinPoint point){String kind = point.getKind();// 获取方法参数传入的值Object[] args = point.getArgs();String methodName = point.getSignature().getName();// 前置通知System.out.println("Around LoggingAspect ==> The method " + kind+ " begin with " + Arrays.asList(args));try {// 执行目标方法int result = (int) point.proceed();// 返回通知System.out.println("Around LoggingAspect ==> The method "+Arrays.asList(args)+ " result : " + result);} catch (Throwable throwable) {// 异常通知throwable.printStackTrace();System.out.println("Around LoggingAspect ==> The method "+ methodName + "occurs Exception " + throwable);}finally {// 后置通知System.out.println("Around LoggingAspect ==> The method "+methodName+" end with " + Arrays.asList(args));}}
}

分析 :

这里 我直接定位到 报错位置 打上断点
在这里插入图片描述
然后 debug 运行
在这里插入图片描述
在这里插入图片描述
从 debug 的情况来看可以得出 , add 方法的返回值类型时 int 类型而不是 void 类型 下图 显示不匹配
在这里插入图片描述

解决方法

在这里插入图片描述
使用环绕通知时 返回值 如果调用的方法是是有返回值的 ,
在这里插入图片描述
如果没有返回值 则改成 void 即可 , 使用返回通知时 返回值的类型和 调用方法的返回值的类型 没有关系 所以即使是 void 也可以正常运行
在这里插入图片描述

这篇关于【Spring】- Spring 返回通知报错 Null return value from advice does not match primitive return type for ⭐️⭐️的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/309542

相关文章

Java对象和JSON字符串之间的转换方法(全网最清晰)

《Java对象和JSON字符串之间的转换方法(全网最清晰)》:本文主要介绍如何在Java中使用Jackson库将对象转换为JSON字符串,并提供了一个简单的工具类示例,该工具类支持基本的转换功能,... 目录前言1. 引入 Jackson 依赖2. 创建 jsON 工具类3. 使用示例转换 Java 对象为

SpringBoot快速接入OpenAI大模型的方法(JDK8)

《SpringBoot快速接入OpenAI大模型的方法(JDK8)》本文介绍了如何使用AI4J快速接入OpenAI大模型,并展示了如何实现流式与非流式的输出,以及对函数调用的使用,AI4J支持JDK8... 目录使用AI4J快速接入OpenAI大模型介绍AI4J-github快速使用创建SpringBoot

Java中的Cursor使用详解

《Java中的Cursor使用详解》本文介绍了Java中的Cursor接口及其在大数据集处理中的优势,包括逐行读取、分页处理、流控制、动态改变查询、并发控制和减少网络流量等,感兴趣的朋友一起看看吧... 最近看代码,有一段代码涉及到Cursor,感觉写法挺有意思的。注意是Cursor,而不是Consumer

解决java.lang.NullPointerException问题(空指针异常)

《解决java.lang.NullPointerException问题(空指针异常)》本文详细介绍了Java中的NullPointerException异常及其常见原因,包括对象引用为null、数组元... 目录Java.lang.NullPointerException(空指针异常)NullPointer

javaScript在表单提交时获取表单数据的示例代码

《javaScript在表单提交时获取表单数据的示例代码》本文介绍了五种在JavaScript中获取表单数据的方法:使用FormData对象、手动提取表单数据、使用querySelector获取单个字... 方法 1:使用 FormData 对象FormData 是一个方便的内置对象,用于获取表单中的键值

前端知识点之Javascript选择输入框confirm用法

《前端知识点之Javascript选择输入框confirm用法》:本文主要介绍JavaScript中的confirm方法的基本用法、功能特点、注意事项及常见用途,文中通过代码介绍的非常详细,对大家... 目录1. 基本用法2. 功能特点①阻塞行为:confirm 对话框会阻塞脚本的执行,直到用户作出选择。②

SpringBoot项目注入 traceId 追踪整个请求的日志链路(过程详解)

《SpringBoot项目注入traceId追踪整个请求的日志链路(过程详解)》本文介绍了如何在单体SpringBoot项目中通过手动实现过滤器或拦截器来注入traceId,以追踪整个请求的日志链... SpringBoot项目注入 traceId 来追踪整个请求的日志链路,有了 traceId, 我们在排

Java实战之利用POI生成Excel图表

《Java实战之利用POI生成Excel图表》ApachePOI是Java生态中处理Office文档的核心工具,这篇文章主要为大家详细介绍了如何在Excel中创建折线图,柱状图,饼图等常见图表,需要的... 目录一、环境配置与依赖管理二、数据源准备与工作表构建三、图表生成核心步骤1. 折线图(Line Ch

Spring Boot 3 整合 Spring Cloud Gateway实践过程

《SpringBoot3整合SpringCloudGateway实践过程》本文介绍了如何使用SpringCloudAlibaba2023.0.0.0版本构建一个微服务网关,包括统一路由、限... 目录引子为什么需要微服务网关实践1.统一路由2.限流防刷3.登录鉴权小结引子当前微服务架构已成为中大型系统的标

Java集合中的List超详细讲解

《Java集合中的List超详细讲解》本文详细介绍了Java集合框架中的List接口,包括其在集合中的位置、继承体系、常用操作和代码示例,以及不同实现类(如ArrayList、LinkedList和V... 目录一,List的继承体系二,List的常用操作及代码示例1,创建List实例2,增加元素3,访问元