0

My bean is :

@Component
public class KidsServerStartUp implements ServletContextListener
{
     UploadService uplService;

    @Autowired
    public void setUplService( UploadService uplService )
    {
        this.uplService = uplService;
    }
    public void contextInitialized(ServletContextEvent event) {
       System.out.println ( uplService );
    }
}

In web.xml; I am firstly calling spring framework to set all beans ; then setting the startup listener :

<listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <listener>
        <listener-class>com.kids.util.KidsServerStartUp</listener-class>
    </listener>

uplService is getting printed as null !

Deepak Singhal
  • 10,568
  • 11
  • 59
  • 98
  • 1
    You are saying `OptimizeImages` is injected to `UploadService` but your code suggestes `UploadService` is injected to `OptimizeImages`, which one is correct? Where is the task scheduling code written? – Arun P Johny Jul 03 '12 at 03:21
  • Arun, I have done some re-factoring above so that the problem is more obvious. Thanks for looking into it. – Deepak Singhal Jul 03 '12 at 04:07

1 Answers1

3

I think what you are looking for is something like this post.

Since you are using a ServletContextListener spring context will not be used for the creation of the Listener class. But we can get access to the ApplicationContext using the ServletContext.

public class KidsServerStartUp implements ServletContextListener {
    public void contextInitialized(ServletContextEvent event) {
        final WebApplicationContext springContext = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());
        UploadService uplService = springContext.getBean(UploadService.class);
        System.out.println ( uplService );
    }
}
Community
  • 1
  • 1
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531