38

i want to use spring autowiring in servlet so here's my code:

@Configurable
public class ImageServlet extends HttpServlet {

   @Autowired
   private SystemPropertyDao systemPropertyDao;

   @Override
   public void init() throws ServletException {


   String imagePath = systemPropertyDao.findByID(StaticParam.CONTENT_FOLDER);

}

while the SystemPropertyDao is annotated with @Repository

and my applicationContext.xml:

<context:component-scan base-package="com.basepackage" />
<mvc:annotation-driven />
<context:annotation-config />
<context:spring-configured/>

web.xml:

  <servlet>
    <servlet-name>imageServlet</servlet-name>
    <servlet-class>com.xeno.basepackage.ImageServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>imageServlet</servlet-name>
    <url-pattern>/myimages/*</url-pattern>
  </servlet-mapping>

sometimes the autowiring works and sometimes it doesn't (the reference to the spring bean systemPropertyDao is null), can anyone please tell me if i am missing something?

Gijs
  • 144
  • 1
  • 1
  • 11
Mahmoud Saleh
  • 33,303
  • 119
  • 337
  • 498
  • 1
    What do you mean by "autowiring doesn't work"? It doesn't work in specific methods for example? – Boris Treukhov Aug 07 '12 at 10:22
  • @Boris Treukhov (the reference to the spring bean systemPropertyDao is null) – Mahmoud Saleh Aug 07 '12 at 10:27
  • 1
    Forgive me my ignorance(somewhat like in the deleted answer) but what is the actual mechanism are you using for injection? Do you call SpringBeanAutowiringSupport.processInjectionBasedOnServletContext() anywhere? – Boris Treukhov Aug 07 '12 at 10:40
  • If you configured the servlet in web.xml there might be more than one instance of it. – Stefan Aug 07 '12 at 10:43
  • @Stefan Lindenberg, yes i did configured the servlet in web.xml, please see my the updated question, any ideas how to solve this case ? – Mahmoud Saleh Aug 07 '12 at 10:45
  • @Boris Treukhov, i don't understand the questions really. – Mahmoud Saleh Aug 07 '12 at 10:45
  • 1
    @Msaleh There not many ways to access Spring beans from the servlet class (http://stackoverflow.com/questions/467235/access-spring-beans-from-a-servlet-in-jboss) so it's interesting which way you've chosen. In the simplest case you may be trying to access the bean before calling SpringBeanAutowiringSupport.processInjectionBasedOnServletContext so it's null. – Boris Treukhov Aug 07 '12 at 10:50

2 Answers2

78

I followed the solution in the following link, and it works fine: Access Spring beans from a servlet in JBoss

public class MyServlet extends HttpServlet {

  @Autowired
  private MyService myService;

  public void init(ServletConfig config) {
    super.init(config);
    SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this,
      config.getServletContext());
  }
}
Community
  • 1
  • 1
Mahmoud Saleh
  • 33,303
  • 119
  • 337
  • 498
  • thanks very much. At first I use `Resource` annotation, it doesn't work, and `Autowired` works well. – hiway Oct 05 '13 at 07:13
  • Thank you very much, but why they specifically mentioned in jBoss?? Won't it work in weblogic?? – Suryaprakash Pisay Aug 13 '14 at 13:46
  • It will work in Weblogic and even in Tomcat ! This is only the title of the documentation: the solution is actually relying upon the standard servlet api and pure Spring classes. – Chucky Apr 01 '15 at 08:49
28

Remove the @Configurable annotation from your servlet and add:

SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext (this);

at the first line of your init() method.

Stefan
  • 12,108
  • 5
  • 47
  • 66