3

I can't seem to get my servlet's fields to @AutoWire; they end up null. I have a pure annotation-configured webapp (no XML files). My servlet looks like this:

@WebServlet("/service")
public
class
SatDBHessianServlet
    extends HttpServlet
{
    @Autowired  protected   NewsItemDAO             mNewsItemDAO;
}

Other @AutoWired things seem to work fine, both @Service objects and @Repository objects. But not this one, and I can't figure out why. I even tried adding its package to the ComponentScan(basePackages) list for my other classes.

Additional Info:

I added the following to my servlet’s init() method, and everything seemed to wire up properly, but I'm confused as to why Spring can't wire it up without that.

SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, inConfig.getServletContext());
Rick
  • 3,298
  • 3
  • 29
  • 47
  • possible duplicate of [Why is my Spring @Autowired field null?](http://stackoverflow.com/questions/19896870/why-is-my-spring-autowired-field-null) – chrylis -cautiouslyoptimistic- Mar 10 '14 at 05:23
  • Well, that question deals with objects instantiated with new, which is not what I'm doing here, although since Spring isn't instantiating it, either, I guess that might be equivalent. I'll try their solution with @Configurable. – Rick Mar 10 '14 at 08:17
  • The situation is the same; `new` is being called by something that the Spring container doesn't know to handle (in this case, directly by the servlet container). Why are you hand-writing a servlet instead of using a framework support? – chrylis -cautiouslyoptimistic- Mar 10 '14 at 08:26

2 Answers2

4

Servlet are web components that are not being created by Spring container, based on that lifecycle is not managed by the container and a lot of stuff that spring provides such as autowired or aspect can not run from them.

You need to indicate to the spring container that a component was created outside of IoC container and need to be part of it.

And as the API said SpringBeanAutowiringSupport is for:

Convenient base class for self-autowiring classes that gets constructed within a Spring-based web application

This generic servlet base class has no dependency on the Spring ApplicationContext concept.

There is another way to indicate servlets being created by spring container using an interface

Koitoer
  • 18,778
  • 7
  • 63
  • 86
  • Like I said, I added it to the ComponentScan(basePackages), so Spring should've been able to find it. Is that not how it works? – Rick Mar 10 '14 at 08:15
  • Not at all componentScan works with @Component and further stereotype annotations, WebServlet annotation is not one of them, so Spring container is not aware and not control the lifecycle of it. – Koitoer Mar 10 '14 at 15:44
1

Spring MVC uses the DispatcherServlet for handling all requests in a Servlet environment. The DispatcherServlet accordingly forwards the requests to the appropriate @Controller class (if any) based on the @RequestMapping on that class and/or it's methods.

What is happening in your Servlet is that it is not managed by Spring (but by the Servlet container) and there for no injection of dependencies is occurring.

geoand
  • 60,071
  • 24
  • 172
  • 190