How to connect HttpServlet with Spring Application Context in web.xml?

后端 未结 2 1185
死守一世寂寞
死守一世寂寞 2020-12-18 06:41

I\'m trying to connect my FooServlet which extends HttpServlet with the ApplicationContext which is in the same Project. The Application Context is already used by a Wicket

2条回答
  •  借酒劲吻你
    2020-12-18 06:58

    I found a way to inject Beans in my HttpServlet (Note: I don't need a Presentation View, otherwise there are more advanced Spring Classes)

    Add a ContextLoaderListener to web.xml so that Spring's root WebApplicationContext is loaded

       
          
            org.springframework.web.context.ContextLoaderListener
          
       
    

    Configure Servlet using Springs HttpRequestHandlerServlet Class

    
     FooServlet
     Foo Servlet
     
          org.springframework.web.context.support.HttpRequestHandlerServlet
        
    
    

    Let your Servlet implement the org.springframework.web.HttpRequestHandler Interface

    Define your Servlet as a Bean in ApplicationContext (beanID must be same as "servlet-name"). Now it's possible to inject all necassary Beans in the Spring DependencyInjection way without dependency lookup.

提交回复
热议问题