@Autowired HttpServletRequest vs passing as parameter - best practice

后端 未结 1 709
夕颜
夕颜 2020-12-28 09:16

Assume we have defined a controller class which has @Controller annotation only.

Inside the class, we have defined private @Autowired HttpServletR

相关标签:
1条回答
  • 2020-12-28 09:23

    Both are ok.
    @Autowired HttpServletRequest and passing as a parameter are the same things.

    Before passing HttpServletRequest to invocation method responding to @RequestMapping function, Spring stores the HttpServletRequest into a ThreadLocal type variable.

    That ThreadLocal variable is a thread-safe map that keeps HttpServletRequest in the current thread context. The @Autowired HttpServletRequest proxy bean gets the correct request from that ThreadLocal variable.

    0 讨论(0)
提交回复
热议问题