首页 > 解决方案 > @Around(AOP) 的 joinPoint 继续但不返回结果

问题描述

我使用@Around 用springboot 实现AOP。像下面

@Around("cut()")
public void advice( ProceedingJoinPoint proceedingJoinPoint ) throws Throwable {
    System.out.println("@Around start");
    proceedingJoinPoint.proceed();
    System.out.println("@Around end");
}

joinPoint 是我的控制器 & The"@Mylog"是我的自定义注释。如下所示

@MyLog
@RequestMapping("/log")
public String getLog() throws InterruptedException {
    System.out.println("This is joinPoint");
    return "Hello World";
}

当我尝试使用浏览器获取路由“/log”时,信息按预期打印,但没有任何内容返回到浏览器(我希望"Hello World"会返回)。像下面

@Around start
This is joinPoint
@Around end

有什么建议吗?

标签: spring-bootaop

解决方案


如果你的 around 建议没有返回任何 ( void) 而不是proceed()结果,那么你应该不会感到惊讶,那么实际上什么也没有返回。;-) 这个怎么样?

@Around("cut()")
public Object advice(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
    System.out.println("@Around start");
    try {
        return proceedingJoinPoint.proceed();
    }
    finally {
        System.out.println("@Around end");
    }
}

推荐阅读