5

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 Servlet

It works with

servletContext = this.getServletContext();
wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
(IMyBean)wac().getBean("myServiceBean")

Now I try to aviod to use explicitly Spring Classes in my Code (WebApplicationContextUtils) as it's not the IoC way.

The Wicket Servlet is connected with the Application context in the web.xml

<servlet>
  <servlet-name>ExampleApplication</servlet-name>
  <servlet-class>org.apache.wicket.protocol.http.WicketServlet</servlet-class>
    <init-param>
      <param-name>applicationFactoryClassName</param-name>
      <param-value>org.apache.wicket.spring.SpringWebApplicationFactory</param-value>
    </init-param>
  <load-on-startup>1</load-on-startup>
</servlet>

I found the class Spring HttpServletBean but I don't know if it serves for my Case

Martin Dürrmeier
  • 1,653
  • 5
  • 18
  • 35
  • I'm unclear as to what you're trying to achieve - you want to connect your servlet with the app context, but without putting Spring code in the servlet? – skaffman Dec 08 '09 at 13:30
  • exactly, I guess there is a way to connect FooServlet with ApplicationContext in the web.xml. I know how it works for Wicket and Spring MVC but not for a Basic HttpServlet – Martin Dürrmeier Dec 08 '09 at 14:01
  • 1
    But Spring MVC and Wicket both contain Spring code. How do you expect to access the Spring context without Spring code in your servlet? – skaffman Dec 08 '09 at 14:41
  • I thought there is a class like org.apache.wicket.spring.SpringWebApplicationFactory for normal HttpServlets without Wicket. – Martin Dürrmeier Dec 08 '09 at 15:13
  • Martin Dürrmeiers answer confirmed at springsource http://forum.springsource.org/showthread.php?t=75289 –  Apr 28 '10 at 11:35

2 Answers2

16

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

   <listener>
      <listener-class>
        org.springframework.web.context.ContextLoaderListener
      </listener-class>
   </listener>

Configure Servlet using Springs HttpRequestHandlerServlet Class

<servlet>
 <servlet-name>FooServlet</servlet-name>
 <display-name>Foo Servlet</display-name>
 <servlet-class>
      org.springframework.web.context.support.HttpRequestHandlerServlet
    </servlet-class>
</servlet>

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.

Martin Dürrmeier
  • 1,653
  • 5
  • 18
  • 35
  • 1
    Martin, do you know of any way to inject an HttpServlet (whose source cannot change) into a Spring Web App DAO class? ... I have an HttpServlet that I need to initialize data and I need to call it when my app is deployed. It's been quite a problem for me. Any thoughts? – blong Apr 18 '11 at 04:45
  • Thanks Paŭlo, the formatting was somehow broken, it's the same as servlet-name – Martin Dürrmeier Nov 13 '13 at 15:39
  • You also need a – Adriaan Koster Jul 30 '14 at 10:18
  • @MartinDürrmeier- This is for single servlet...what if i have multiple servlets in my application? – Anand Dec 16 '14 at 17:53
2

I think you should use Spring utilities like RequestContextUtils.getWebApplicationContext(request, application); to hookup the Spring Context within your Servlet. Agreed this is no DI/IoC, but the servlet is no bean as well !

Shreeni
  • 295
  • 4
  • 12