Spring在service层获取session和request
Posted caozx
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring在service层获取session和request相关的知识,希望对你有一定的参考价值。
获取request有两种方法:
-
1.在web.xml文件中添加监听器,然后通过注解注入request:
<listener> <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> </listener>
然后在service层注入直接注入对象@Autowired private HttpSession session; @Autowired private HttpServletRequest request;
-
2.直接在service层普通类中获取
HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();