Web容器启动时加载Spring分析
来源:互联网 发布:南京凶宅数据库 编辑:程序博客网 时间:2024/06/08 18:34
在应用程序web.xml中做了以下配置信息时,当启动Web容器时就会自动加载Spring容器。
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
ContextLoaderListener类实现了javax.servlet.ServletContextListener接口并且继承了org.springframework.web.context.ContextLoader类。ServletContextListener事件类是Web容器的一部分,处理Web应用的Servlet上下文(context)的监听。实现ServletContextListener接口中的contextInitialized和contextDestroyed方法。当Web容器启动时会自动调用contextInitialized方法,进行初始化Spring Web应用程序上下文,主要加载web.xml中contextConfigLocation的配置文件;当Web容器关闭之前会调用contextDestroyed方法,进行销毁Spring Web应用程序上下文。ContextLoader类实现了Spring上下文初始化的工作,执行initWebApplicationContext方法返回WebApplicationContext。Spring实现的contextInitialized和contextDestroyed代码如下:
- public void contextInitialized(ServletContextEvent event) {
- this.contextLoader = createContextLoader();
- if (this.contextLoader ==null) {
- this.contextLoader = this;
- }
- this.contextLoader.initWebApplicationContext(event.getServletContext());
- }
- public void contextDestroyed(ServletContextEvent event) {
- if (this.contextLoader !=null) {
- this.contextLoader.closeWebApplicationContext(event.getServletContext());
- }
- ContextCleanupListener.cleanupAttributes(event.getServletContext());
- }
Spring执行实现Spring上下文的方法
- public WebApplicationContext initWebApplicationContext(ServletContext servletContext) {
- if (servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) !=null) {
- throw new IllegalStateException(
- "Cannot initialize context because there is already a root application context present - " +
- "check whether you have multiple ContextLoader* definitions in your web.xml!");
- }
- Log logger = LogFactory.getLog(ContextLoader.class);
- servletContext.log("Initializing Spring root WebApplicationContext");
- if (logger.isInfoEnabled()) {
- logger.info("Root WebApplicationContext: initialization started");
- }
- long startTime = System.currentTimeMillis();
- try {
- // Store context in local instance variable, to guarantee that
- // it is available on ServletContext shutdown.
- if (this.context ==null) {
- this.context = createWebApplicationContext(servletContext);
- }
- if (this.context instanceof ConfigurableWebApplicationContext) {
- configureAndRefreshWebApplicationContext((ConfigurableWebApplicationContext)this.context, servletContext);
- }
- servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,this.context);
- ClassLoader ccl = Thread.currentThread().getContextClassLoader();
- if (ccl == ContextLoader.class.getClassLoader()) {
- currentContext = this.context;
- }
- else if (ccl != null) {
- currentContextPerThread.put(ccl, this.context);
- }
- if (logger.isDebugEnabled()) {
- logger.debug("Published root WebApplicationContext as ServletContext attribute with name [" +
- WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE + "]");
- }
- if (logger.isInfoEnabled()) {
- long elapsedTime = System.currentTimeMillis() - startTime;
- logger.info("Root WebApplicationContext: initialization completed in " + elapsedTime + " ms");
- }
- return this.context;
- }
- catch (RuntimeException ex) {
- logger.error("Context initialization failed", ex);
- servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ex);
- throw ex;
- }
- catch (Error err) {
- logger.error("Context initialization failed", err);
- servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, err);
- throw err;
- }
- } 踩
0 0
- Web容器启动时加载Spring分析
- Web容器启动时加载Spring
- Web容器启动时加载Spring的配置
- web容器启动spring容器
- web容器启动Spring
- WEB启动时,如何加载spring容器和struts2容器(bean的注入和url如何请求)
- Spring源码分析之ioc容器第二天之oC容器在Web容器中的启动
- Tomcat启动时加载数据到缓存---web.xml中listener加载顺序(优先初始化Spring IOC容器)
- Tomcat启动时加载数据到缓存--Web.xml里listener的加载顺序,优先初始化Spring IOC容器
- web项目启动Spring容器
- Web容器 与Spring容器 启动
- Spring源代码分析之(二):IOC容器在web容器中的启动
- spring在web容器启动时执行初始化方法
- spring在web容器启动时执行初始化方法
- spring在web容器启动时执行初始化方法
- Spring MVC让Web容器启动时自动执行代码
- spring在web容器启动时执行初始化方法
- spring 在web容器启动时执行初始化方法
- 模型自动创建
- 土豆肉丝做法
- Qt 获取Windows内存信息
- 解决VMware下64位Linux系统不支持全虚拟化(VMX)
- android--fragment的使用
- Web容器启动时加载Spring分析
- 安卓获取版本号
- Android SDK Manager目录解析
- c#面向对象特征(2)之多态
- 有一篇文章,共三行文字,每行有最多80个字符。要求分别统计出其中英文大写字母,小写字母,数字,空格及其他字符
- 10 款最佳剪贴板管理器
- C语言数据输入与输出
- cocos2d结合cocostudio适配
- 字符串操作