Spring-AOP环绕监听出错

Exception in thread "main" org.springframework.aop.AopInvocationException: Null return value from advice does not match primitive return type for: public int aoppoint.User.getUsername()
at org.springframework.aop.framework.CglibAopProxy.processReturnType(CglibAopProxy.java:391)
at org.springframework.aop.framework.CglibAopProxy.access$000(CglibAopProxy.java:82)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:675)
at aoppoint.User$$EnhancerBySpringCGLIB$$ab381b5d.getUsername(<generated>)
at aoppoint.Console.main(Console.java:25)

  public void logAround(ProceedingJoinPoint joinPoint) {
        System.out.println("userbean方法执行之前 Around,切入的名字 : " + joinPoint.getSignature().getName());
        try {
            joinPoint.proceed();   //阻塞
            System.out.println("userbean方法执行之后 Around,切入的名字 : " + joinPoint.getSignature().getName());
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            System.out.println("userbean方法执行出错 Around,切入的名字 : " + joinPoint.getSignature().getName());
        }
    }

  后来发现是环绕监听会对有返回值的方法做处理,所以需要对 logAround方法修改一下,让它有一个返回值,返回object就可以了

public Object logAround(ProceedingJoinPoint joinPoint) {
        System.out.println("userbean方法执行之前 Around,切入的名字 : " + joinPoint.getSignature().getName());
        Object result=null;
        try {
            result = joinPoint.proceed();//阻塞
            System.out.println("userbean方法执行之后 Around,切入的名字 : " + joinPoint.getSignature().getName());
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            System.out.println("userbean方法执行出错 Around,切入的名字 : " + joinPoint.getSignature().getName());
        }
        return result;
    }

 

posted @ 2019-03-04 21:13  若冲  阅读(1568)  评论(0编辑  收藏  举报