如何在aop中取得requset
程序员文章站
2022-04-25 20:09:15
...
如何在aop中取得requset
可以利用 ThreadLocal
代码如下
public class ApiContext
{
private static ThreadLocal<Httpservletrequest> requestLocal = new ThreadLocal<Httpservletrequest>();
public static HttpServletRequest getRequestLocal()
{
return requestLocal.get();
}
public static void setRequestLocal(HttpServletRequest request)
{
requestLocal.set(request);
}
aop中取得request
@Around("execution(* atf.pnr.inf.TicketService.add*(..))")
public final Object invoke(final ProceedingJoinPoint pjp) throws Throwable
{
Object retVal = null;
.......
HttpServletRequest request=ApiContext.getRequestLocal();
System.out.println(request.getRemoteAddr())
........
return retVal;
}
上一篇: spring log 日志管理
下一篇: AOP之Hello World