当在Spring AOP中使用@Around时,数据将不会被检索到。

问题描述 投票:0回答:1

我是春天的AOP,我有如下的@Around。

@Around(value = "execution(* com.spring.rest.controller.Controller.*(..))")
public void around(ProceedingJoinPoint joinPoint) throws Throwable {
    long start = System.currentTimeMillis();
    joinPoint.proceed();
    long taken = System.currentTimeMillis() - start;
    logger.info("around this {} time taken is {}", joinPoint, taken);
} 

在我的Rest控制器中,我有getmapping,当我在浏览器中调用该路由的时候 无数据.

从日志中,我发现它正在返回null(以下日志信息) --@Around被执行,花了20秒,@AfterReturning被执行,返回null。

2020-05-07 00:41:03.083  INFO 366372 --- [nio-8080-exec-1] lication$$EnhancerBySpringCGLIB$$21c1be2 : around this execution(List com.spring.rest.controller.Controller.getm()) time taken is 20
2020-05-07 00:41:03.084  INFO 366372 --- [nio-8080-exec-1] lication$$EnhancerBySpringCGLIB$$21c1be2 : returning execution(List com.spring.rest.controller.Controller.getm()) returned with value null

但当我删除@Around时,API却能完美运行。

我可以知道是什么原因和如何解决这个问题吗?

java spring spring-boot spring-aop spring-restcontroller
1个回答
2
投票

你需要返回对象。

public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
    long start = System.currentTimeMillis();
    Object object = joinPoint.proceed();
    long taken = System.currentTimeMillis() - start;
    logger.info("around this {} time taken is {}", joinPoint, taken);
    return object;
} 
© www.soinside.com 2019 - 2024. All rights reserved.