Spring源码分析

xiaoxiao2021-02-28  111

在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; /* */ } /* */ }

转载请注明原文地址: https://www.6miu.com/read-58652.html

最新回复(0)