[英]Get spring application context on a ServletContextListener
I am trying to get the spring application context on a ServletContextListener. 我正在尝试在ServletContextListener上获取spring应用程序上下文。 I am using Spring with annotation configuration.
我正在使用带有注释配置的Spring。 Using this code i get "context null".
使用此代码,我得到“ 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 谢谢
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
解决此问题的关键是删除
@WebListener
注释,并在WebAppInitializer
重写onStartup
以确保在Initializer
之前加载了ContextLoaderListener
public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer
{
.
.
.
@Override
public void onStartup(ServletContext container) throws ServletException
{
super.onStartup(container);
container.addListener(Initializer.class);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.