@Around(AOP)的joinPoint继续执行,但不返回结果

时间:2019-07-30 15:14:34

标签: spring-boot aop

我使用@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是我的控制器,而"@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

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

如果您的周围建议没有任何结果(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");
    }
}