ContextLoaderListener监听器的作用就是启动Web容器时,自动装配ApplicationContext的配置信息;源码如下
package org.springframework.web.context;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class ContextLoaderListener extends ContextLoader
implements ServletContextListener
{
public ContextLoaderListener()
{
}
public ContextLoaderListener(WebApplicationContext context)
{
super(context);
}
public void contextInitialized(ServletContextEvent event)
{
initWebApplicationContext(event.getServletContext());
}
public void contextDestroyed(ServletContextEvent event)
{
closeWebApplicationContext(event.getServletContext());
ContextCleanupListener.cleanupAttributes(event.getServletContext());
}
}
可以看出其实现了ServletContextListener接口中的contextInitialized和contextDestroyed两个方法。
ServletContextListener 是 ServletContext 的监听者,如果 ServletContext 发生变化,如服务器启动时 ServletContext 会被创建,服务器关闭时 ServletContext 将要被销毁。而整个加载过程由ContextLoader来完成,spring中的bean也由其创建。