Reputation: 835
I am trying to get the spring application context on a ServletContextListener. I am using Spring with annotation configuration. Using this code i get "context null". What I am doing wrong?
@WebListener
public class Initializer implements ServletContextListener
{
public void contextInitialized(ServletContextEvent event)
{
System.out.println("context " + WebApplicationContextUtils.getWebApplicationContext(event.getServletContext()));
}
@Override
public void contextDestroyed(ServletContextEvent sce)
{
}
}
Thanks
Upvotes: 1
Views: 1089
Reputation: 835
The key for fix the problem has been delete the annotation @WebListener
and on WebAppInitializer
override onStartup
to ensure that the ContextLoaderListener
is loaded before Initializer
public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer
{
.
.
.
@Override
public void onStartup(ServletContext container) throws ServletException
{
super.onStartup(container);
container.addListener(Initializer.class);
}
}
Upvotes: 2