Spring源码分析

来源:互联网 发布:淘宝店主软件 编辑:程序博客网 时间:2024/06/08 04:25

在Java EE项目中,Spring的载入时通过监听器实现的,无论是在SSH还是SSM,在web.xml都会有这样的配置

<!-- 载入spring配置 --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath*:cn/resources/applicationContext.xml        </param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>
进入ContextLoaderListener这个类

 public class ContextLoaderListener/*     */   extends ContextLoader/*     */   implements ServletContextListener/*     */ {/*     */   public ContextLoaderListener() {}/*     */   /*     */   public ContextLoaderListener(WebApplicationContext context)/*     */   {/*  98 */     super(context);/*     */   }/*     */   /*     */ /*     */ /*     */ /*     */ /*     */   public void contextInitialized(ServletContextEvent event)/*     */   {/* 107 */     initWebApplicationContext(event.getServletContext());/*     */   }/*     */   /*     */ /*     */ /*     */ /*     */ /*     */   public void contextDestroyed(ServletContextEvent event)/*     */   {/* 116 */     closeWebApplicationContext(event.getServletContext());/* 117 */     ContextCleanupListener.cleanupAttributes(event.getServletContext());/*     */   }/*     */ }

contextInitialized与contextDestroyed分别对应着初始化与销毁,我们来看看初始化

public WebApplicationContext initWebApplicationContext(ServletContext servletContext)/*     */   {/* 296 */     if (servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) != null) {/* 297 */       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!");/*     */     }/*     */     /*     */ /*     */ /* 302 */     Log logger = LogFactory.getLog(ContextLoader.class);/* 303 */     servletContext.log("Initializing Spring root WebApplicationContext");/* 304 */     if (logger.isInfoEnabled()) {/* 305 */       logger.info("Root WebApplicationContext: initialization started");/*     */     }/* 307 */     long startTime = System.currentTimeMillis();/*     */     /*     */ /*     */     try/*     */     {/* 312 */       if (this.context == null) {/* 313 */         this.context = createWebApplicationContext(servletContext);/*     */       }/* 315 */       if ((this.context instanceof ConfigurableWebApplicationContext)) {/* 316 */         ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext)this.context;/* 317 */         if (!cwac.isActive())/*     */         {/*     */ /* 320 */           if (cwac.getParent() == null)/*     */           {/*     */ /* 323 */             ApplicationContext parent = loadParentContext(servletContext);/* 324 */             cwac.setParent(parent);/*     */           }/* 326 */           configureAndRefreshWebApplicationContext(cwac, servletContext);/*     */         }/*     */       }/* 329 */       servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);/*     */       /* 331 */       ClassLoader ccl = Thread.currentThread().getContextClassLoader();/* 332 */       if (ccl == ContextLoader.class.getClassLoader()) {/* 333 */         currentContext = this.context;/*     */       }/* 335 */       else if (ccl != null) {/* 336 */         currentContextPerThread.put(ccl, this.context);/*     */       }/*     */       /* 339 */       if (logger.isDebugEnabled()) {/* 340 */         logger.debug("Published root WebApplicationContext as ServletContext attribute with name [" + WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE + "]");/*     */       }/*     */       /* 343 */       if (logger.isInfoEnabled()) {/* 344 */         long elapsedTime = System.currentTimeMillis() - startTime;/* 345 */         logger.info("Root WebApplicationContext: initialization completed in " + elapsedTime + " ms");/*     */       }/*     */       /* 348 */       return this.context;/*     */     }/*     */     catch (RuntimeException ex) {/* 351 */       logger.error("Context initialization failed", ex);/* 352 */       servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ex);/* 353 */       throw ex;/*     */     }/*     */     catch (Error err) {/* 356 */       logger.error("Context initialization failed", err);/* 357 */       servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, err);/* 358 */       throw err;/*     */     }/*     */   }




原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 2岁宝贝拉肚子怎么办 宝宝肠道蠕动慢怎么办 新生儿吃母乳吃不饱怎么办 小孩胃口不好怎么办呢 新生儿吃多了怎么办 宝宝奶量上不去怎么办 新生儿吃撑了怎么办 婴儿不够奶吃怎么办 产妇奶堵了怎么办 新生儿吃奶不吃奶粉怎么办 奶瓶吸奶费力怎么办 小孩上火感冒了怎么办 宝宝上火感冒了怎么办 3岁宝宝上火怎么办 上火又受凉感冒怎么办 上火引起的感冒怎么办 奶瓶排气孔漏水怎么办 奶嘴排气孔漏水怎么办 bbox吸管杯漏水怎么办 四个月宝宝拉肚子怎么办 租的房子坐月子怎么办 榨果汁不甜怎么办 宝宝不会喝奶粉怎么办 两个月宝宝不长肉怎么办 打疫苗后发烧怎么办 孕期不爱吃水果怎么办 孕期很少吃水果怎么办 三个月小孩不吃奶粉怎么办 三个月宝宝偏瘦怎么办 破壁机打果汁有沫怎么办 宝宝7个月坐不稳怎么办 婴儿头睡偏了怎么办天 宝宝不爱趴着怎么办 宝宝喜欢竖着抱怎么办 婴儿抱习惯了怎么办 新生儿总让抱着放下就哭可怎么办 三个月宝宝认人怎么办 三个月的宝宝认生怎么办 一岁半宝宝尿黄怎么办 一岁多宝宝尿少怎么办 1岁宝宝一晚没尿怎么办