【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 {

    @Override
    public int add(int i, int j) {
        System.out.println("The method add be execute");
        int result = i + j;
        return result;
    }

    @Override
    public int sub(int i, int j) {
        System.out.println("The method sub be execute");
        int result = i - j;
        return result;
    }

    @Override
    public int mul(int i, int j) {
        System.out.println("The method mul be execute");
        int result = i * j;
        return result;
    }

    @Override
    public 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 也可以正常运行
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

兀坐晴窗独饮茶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值