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 !

2
  • 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? Commented Jul 3, 2012 at 3:21
  • Arun, I have done some re-factoring above so that the problem is more obvious. Thanks for looking into it. Commented Jul 3, 2012 at 4:07

1 Answer 1

2

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 ); } } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.