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;/* */ }/* */ }
阅读全文
0 0
- Spring 源码分析:ApplicationContext
- Spring 源码分析:ApplicationContext- -
- [转贴] Spring源码分析
- Spring 源码分析
- spring jdbc 源码 分析
- Spring ContextLoaderListener源码分析
- spring学习方法源码分析
- Spring IOC源码分析
- spring源码分析
- Spring ContextLoaderListener源码分析 .
- spring事务管理源码分析
- spring学习方法源码分析
- Spring HttpInvoker源码分析
- spring源码分析概要
- spring部分源码分析
- Spring MVC源码分析
- Spring Security3源码分析
- spring-mvc3 源码分析
- python函数
- js 迭代判断字段非空
- yii框架 隐藏index.php 以及美化URL
- 使用Vue-Router 2实现路由功能
- 修改IAR工程名的办法
- Spring源码分析
- 《Scala入坑笔记》缘起 3天就搞了一个 hello world
- Jsp 基本入门
- BeautifulSoup学习笔记3
- Sql Server、Oracle数据库判断字段是否为空的办法
- MYSQL初学者使用指南
- 微信小程序
- nodejs express 子app功能(路由)
- [LeetCode] 628. Maximum Product of Three Numbers