最近在用aop監聽項目中restapi被調用的功能,在使用
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
的時候一只報attributes為null,
后來在web.xml中加入如下監聽:
<!--將Web容器與spring容器進行整合-->
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
就正常了。原因可能跟spring框架有關(個人猜測:spring將參數轉化后params后,自動清理了servlet,所以要在xml中重新加入RequestContextListener)
