英文:
Delay in spring boot aspect cause to delay in joinpoint method
问题
我有一个简单的切面,如下所示:
@Aspect
@Component
public class ApiCallLogAspect {
@AfterReturning(pointcut = "execution(* com.example.web.rest.api.ApiResource.getAddress(String)) && args(mobile)", argNames = "mobile")
public void endpointAfterReturning(String mobile) throws InterruptedException {
Thread.sleep(10000);
System.out.println("This is aspect log.");
}
}
这是ApiResource
类中的getAddress
方法,正如您所看到的,这是一个非常简单且快速的方法:
@RestController
@RequestMapping("/api/v1")
public class ApiResource {
public String getAddress(String mobile) {
return "This is your address";
}
}
现在,当我调用getAddress
方法时,切面中的Thread.sleep(10000)
会导致getAddress
方法的执行延迟。
我如何将切面的执行与连接点方法分开?
注意
我使用了Thread.sleep(10000)
来演示我的问题。我想在方法执行后执行一些操作,而且不想影响主方法,无论是延迟、异常还是其他任何情况。为了达到这个目的,我应该使用什么?
更新
我的目标是记录对REST API的请求并存储调用详细信息,例如IP、参数值、请求头和返回值。
英文:
I have a simple aspect as you can see below:
@Aspect
@Component
public class ApiCallLogAspect {
@AfterReturning(pointcut = "execution(* com.example.web.rest.api.ApiResource.getAddress (String)) && args(mobile))", argNames = "mobile")
public void endpointAfterReturning(String mobile) throws InterruptedException {
Thread.sleep(10000);
System.out.println("This is aspect log.");
}
}
}
And this is getAddress
method in ApiResource
class, as you see this is very simple and fast method:
@RestController
@RequestMapping("/api/v1")
public class ApiResource {
public String getAddress(String mobile) {
return "This is your address";
}
}
Now, when i call getAddress
method the Thread.sleep(10000)
in aspect cause to delay in execution of getAddress
method.
How can i seprate execution of aspect from joinpoint method?
NOTE
I used Thread.sleep(10000)
to demonstrate my problem. I want to do something after a method executed, and don't want to affect on main method, either delay, exception or anything. what should i use to do for this purpose?
UPDATE
My goal is to log requests to my rest apis and store call details, such as ip, arguments values, request headers and returned value.
专注分享java语言的经验与见解,让所有开发者获益!
评论