0

I want to inject a DAO dependency on my class that extends HttpServlet, is this even possible? I don't want to manually obtain the dependency from Application Context, but to have, if possible, real dependency injection on my Servlet instance. I tried annotating my Servlet with @Controller:

@Controller
public class ItemsController extends HttpServlet {

    @Autowired
    private IItemDAO itemDAO;


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Item> items= itemDAO.getItems();
        req.setAttribute("items", items);
        gotoPage("/jsp/itemList.jsp", req, resp);
    }

    protected void gotoPage(String address, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(address);
        dispatcher.forward(request, response);
    }

}

My application Context:

<context:annotation-config />
    <context:component-scan base-package="com.test.controller" />
    <bean id="itemsController" class="com.test.controller.ItemsController " />
    <bean id="itemDAO" class="com.test.dao.ItemDAO" />

Now to my understanding my Servlet (defined in web.xml) is not managed by Spring so my DAO dependency is not getting properly injected, how can I make Spring manage this bean?

MichelReap
  • 5,630
  • 11
  • 37
  • 99

2 Answers2

2

Now to my understanding my Servlet (defined in web.xml) is not managed by Spring so my DAO dependency is not getting properly injected

That's right. A Servlet is a component managed by a Servlet container. A @Controller bean is a component managed by Spring. They are two (usually) conflicting concepts. You should separate them.

Since @Controller is just an annotation, you can have a @Controller bean of type HttpServlet but it will not be managed or used by the Servlet container (or vice-versa).

If you want a Servlet which has injection targets, you can use the solutions provided here.

Community
  • 1
  • 1
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • so there's no way to achieve DI in a regular Servlet? – MichelReap Jul 07 '14 at 19:36
  • @MichelReap I've added a link that provides a solution to DI. But my point is that your `Servlet` should not also act as a `@Controller`. – Sotirios Delimanolis Jul 07 '14 at 19:36
  • Thanks, I'll have a look at those solutions. Correct me if I'm wrong but the `@Controller` annotation doesn't do anything to the bean apart from declaring it as a component, does it? – MichelReap Jul 07 '14 at 19:41
  • @MichelReap It will be picked up by component scanning. The MVC stack (`DispatcherServlet` and `HandlerMapping`s) also processes any beans of type `@Controller` to register any handler methods. – Sotirios Delimanolis Jul 07 '14 at 19:43
0

Another more lightweight solution is based on the HttpRequestHandler. See this blog for a detailed discussion.

Harald Wellmann
  • 12,615
  • 4
  • 41
  • 63