欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

如何在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;
}

 

 

相关标签: AOP