Results 1 to 2 of 2

Thread: Autowiring in servlet

  1. #1
    Join Date
    Oct 2011
    Posts
    18

    Default Autowiring in servlet

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

    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
    Code:
    SystemPropertyDao
    is annotated with
    Code:
    @Repository
    and my applicationContext.xml:

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

    Code:
          <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, can anyone please tell me if i am missing something ?
    Last edited by msaleh; Aug 7th, 2012 at 05:47 AM.

  2. #2
    Join Date
    Mar 2007
    Posts
    561

    Default

    Do not manage servlets by spring - in your example you create 2 instances, one created by the servlet container and one by spring.
    Just use a POJO with @Controller instead.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •