Search code examples
javaspringautowiredconfigurableeclipse-virgo

@Autowired issue with @Configurable servlet


I am trying to autowire a class into a WebSocketServlet in the following way:

@Configurable(autowire=Autowire.BY_TYPE)
public class MyServlet extends WebSocketServlet {
    @Autowired
    public MyClass field;

    // etc...
}

Here's what my configuration looks like:

<context:annotation-config />
<context:component-scan base-package="org.*" />

<bean id="config" class="org.*.MyClass">
   <!-- a bunch of properties -->
</bean>

Note that autowire used to work just fine as long as I was in a Spring @Controller. I had to step out of that because i don't know how to map a WebSocketsServlet to a method of the @Controller as you do with regular servlets.

Any idea what I might be missing?


Solution

  • Getting rid of @Configurable and doing the following in the servlet init method does the trick:

    @Override
    public void init() throws ServletException {
       super.init();
       SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
    }