1

There are many Dependency Injection (DI) methods such as declare bean in XML, @annotation, setter or constructor.. etc.
The HttpSession without any DI method like above, how does it inject into Spring context?
I saw lots of codes that inject HttpSession or Request directly by @Autowired like below:

@Controller
public class MyController{

@Autowired
private HttpSession session;

@RequestMapping("/hello")
    return "hello world";
}

I can't understand how does the HttpSession inject into the controller without any bean injected into context.xml or annotated by @Component.

Can anyone give me the detail of the reason or link about that?

A: method "registerWebApplicationScopes" in WebApplicationContextUtils.java registers such HttpRequest by ConfigurableListableBeanFactory. Also, the method findAutowiredCandicate in DefaultListableBeanFactory shows how does such beans inject into spring context.

stephCurry
  • 91
  • 6
  • @Sotirios Delimanolis It's not the same question. I've googled a lot but can not find one the solve my confusion thus I post it. – stephCurry Sep 26 '18 at 09:52
  • Can you explain what you think is missing from the accepted answer? It explains in detail how a `HttpServletRequest` can be injected. The behavior is the same for `HttpSession`. – Sotirios Delimanolis Sep 26 '18 at 14:12

0 Answers0