Announcement Announcement Module
Collapse
No announcement yet.
Autowiring in servlet Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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, 05:47 AM.

  • #2
    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.

    Comment

    Working...
    X