How to inject Spring beans into Servlets

This can be achieved in 3 simple steps:

1. Implement HttpRequestHandler

First of all your servlet class must implement the org.springframework.web.HttpRequestHandlerinterface and provide an implementation for the handleRequest() method just like you would override doPost().

2. Declare the servlet as a Spring Bean

You can do this by either adding the @Component(“myServlet”) annotation to the class, or declaring a bean with a name myServlet in applicationContext.xml.

   @Component("myServlet")
   public class MyServlet implements HttpRequestHandler {
...

3. Declare in web.xml a servlet named exactly as the Spring Bean

The last step is to declare a new servlet in web.xml that will have the same name as the previously declared Spring bean, in our case myServlet. The servlet class must be org.springframework.web.context.support.HttpRequestHandlerServlet.

           
        <servlet>     
                <display-name>MyServlet</display-name>
                <servlet-name>myServlet</servlet-name>
                <servlet-class>
   org.springframework.web.context.support.HttpRequestHandlerServlet
               </servlet-class>
        </servlet>

        <servlet-mapping>
                <servlet-name>myServlet</servlet-name>
                <url-pattern>/myurl</url-pattern>
        </servlet-mapping>

Now you are ready to inject any spring bean in your servlet class.

   @Component("myServlet") 
   public class MyServlet implements HttpRequestHandler {

        @Autowired
        private MyService myService;

Recommendations




About these ads

5 thoughts on “How to inject Spring beans into Servlets

  1. Pingback: How to display images from the database in your jsp pages – the easy way « Inside Coding

  2. Technically, you cannot inject Spring beans in servlets because the servlet API has no coupling with Spring API. A more correct title is: “How to define a servlet as a Spring-bean”.

    A simple servlet should perform a Spring-component look-up – by using WebApplicationContextUtils.getWebApplicationContext()

    • Technically, yes. This is the reason why the servlet is not declard with the actual class in web.xml, but with org.springframework.web.context.support.HttpRequestHandlerServlet. You can perform a manual lookup, but you will loose the application consistency/readability in my opinion: for some classes you use Annotations/Autowire/etc, for others you perform manual lookup.... More elegant this way :)

  3. :) I repeat, your solution is very good, but it’s not solving the mentioned problem: “how to inject Spring beans into servlet”. It’s all about exposing a Spring bean as a servlet (or how to implement a servlet with Spring API).

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s