spring 容器加载源文案解释(第一章ContextLoaderListener )

 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());
	}

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值