Dependency Injection inside of HttpSessionListener implementation

前端 未结 2 1955
余生分开走
余生分开走 2021-01-07 03:45

Problem: This injected dependency will always return 0 from SimpleController

  1. Why does the context get lost for this bean when try
2条回答
  •  执念已碎
    2021-01-07 04:12

    When you declare a in web.xml like so

    
        com.stuff.morestuff.SessionCounterListener
    
    

    you are telling your Servlet container to instantiate the class specified in the listener-class element. In other words, this instance will not be managed by Spring and it will therefore not be able to inject anything and the field will remain null.

    There are workarounds to this. And some more.

    Note that this

    
    
    

    is not a valid entry in web.xml. I don't know if that was a copy mistake on your part.

提交回复
热议问题