ContextLoaderListener
package org.springframework.web.context;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
/**
* 引导监听器启动和关闭Spring的root {@link WebApplicationContext}。
* 简单地委托给{@link ContextLoader}和{@link ContextCleanupListener}。
* 这个监听器应该在{@link org.springframework.web.util.Log4jConfigListener}之后注册。
* 在{@code web。xml},如果使用后者。*
* 从Spring 3.1开始,{@code ContextLoaderListener}支持注入根web
*应用程序上下文通过{@link #ContextLoaderListener(WebApplicationContext)}构造函数,允许在Servlet 3.0+环境中编程配置。
*参见{@link org.springframework.web。用于使用示例的WebApplicationInitializer}。
* See {@link org.springframework.web.WebApplicationInitializer} for usage examples.
*
* @author Juergen Hoeller
* @author Chris Beams
* @since 17.02.2003
* @see #setContextInitializers
* @see org.springframework.web.WebApplicationInitializer
* @see org.springframework.web.util.Log4jConfigListener
*/
public class ContextLoaderListener extends ContextLoader implements ServletContextListener {
/**
* 创建一个新的{@code ContextLoaderListener},它将基于“contextClass”和“contextConfigLocation”servlet上下文-params创建一个web应用程序上下文。
参见{@link ContextLoader}超类文档了解每个类的默认值的详细信息。当在{@code web内声明{@code ContextLoaderListener}为{@code }时,通常使用这个构造函数。,
* 其中需要一个无参数构造函数。创建的应用程序上下文将以属性名{@link WebApplicationContext#ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE}
* 注册到ServletContext中,当在这个侦听器上调用{@link #contextDestroyed}生命周期方法时,Spring应用程序上下文将被关闭。
* @see ContextLoader
* @see #ContextLoaderListener(WebApplicationContext)
* @see #contextInitialized(ServletContextEvent)
* @see #contextDestroyed(ServletContextEvent)
*/
public ContextLoaderListener() {
}
/**
* 使用给定的应用程序上下文创建一个新的{@code ContextLoaderListener}。这个构造函数在Servlet 3.0+环境中非常有用,在这种环境中,可以通过{@link javax.servlet基于实例注册监听器。ServletContext # addListener} API。上下文可能是,也可能不是{@linkplain org.springframework.context.ConfigurableApplicationContext#refresh() refresh}。如果(a)是{@link nfigurableWebApplicationContext}的实现,而(b)尚未刷新(推荐的方法),则会发生以下情况:如果尚未为给定上下文分配{@linkplain org.springframe .context。ConfigurableApplicationContext#setId id},其中一个对象将被分配给它{@code ServletContext},而{@code ServletConfig}对象将被委托给应用程序上下文{@link #customizeContext}将被调用任何{@link org.springframe .context。将应用通过“contextInitializerClasses”init-param指定的ApplicationContextInitializer}。{@link org.springframework.context。ConfigurableApplicationContext #刷新刷新()}将称为如果上下文已经刷新或没有实现{@code ConfigurableWebApplicationContext},以上假设下会发生用户执行这些操作(或不)他或她的具体需求。看到{@link org.springframework.web。用于使用示例的WebApplicationInitializer}。在任何情况下,给定的应用程序上下文都将以属性名{@link WebApplicationContext#ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE}注册到ServletContext中,当在这个侦听器上调用{@link #contextDestroyed}生命周期方法时,Spring应用程序上下文将被关闭。
* @param context the application context to manage
* @see #contextInitialized(ServletContextEvent)
* @see #contextDestroyed(ServletContextEvent)
*/
public ContextLoaderListener(WebApplicationContext context) {
super(context);
}
/**
* 初始化根web应用程序上下文。
*/
@Override
public void contextInitialized(ServletContextEvent event) {
initWebApplicationContext(event.getServletContext());
}
/**
* 关闭根web应用程序上下文。
*/
@Override
public void contextDestroyed(ServletContextEvent event) {
closeWebApplicationContext(event.getServletContext());
ContextCleanupListener.cleanupAttributes(event.getServletContext());
}
}