如何在aop中取得requset

如何在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)