Reputation: 10876
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 !
Upvotes: 0
Views: 1411
Reputation: 388416
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 );
}
}
Upvotes: 2