深入理解Spring源码:ContextLoaderListener的初始化过程剖析
前言
本文转自“天河聊技术”微信公众号
从本篇文章开始主要介绍spring源码解析相关的spring上下文初始化、bean定义解析、beanFactory创建、初始化、bean定义注册到beanFactory、bean实例化、依赖注入流程中相关的步骤,由于spring源码体系比较庞大,本次主要是跟着程序加载顺序整理,遇到相关关键知识点会单独出来一篇文章,之道这个整个链路顺序解析完毕,后面spring源码合集整理的时候会按照模块进行归类梳理。
正文
本次主要从ContextLoaderListener这个入口进行跟踪源码,进行spring的源码解析。
先简单看下主要的类图
1 bean定义解析相关
2 spring上下文相关
3 beanProcessor相关
beanProcessors对bean的过程管理抽象。
3spring容器加载
跟踪这个方法
org.springframework.web.context.ContextLoaderListener#contextInitialized
servlet监听器初始时会加载初始化web应用程序上下文这个方法
@Override public void contextInitialized(ServletContextEvent event) { initWebApplicationContext(event.getServletContext()); }
跟踪这个方法
org.springframework.web.context.ContextLoader#initWebApplicationContext
public WebApplicationContext initWebApplicationContext(ServletContext servletContext) { // web上下文是放在servlet上下文中的 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. // 初始化web程序上下文 if (this.context == null) { this.context = createWebApplicationContext(servletContext); } if (this.context instanceof ConfigurableWebApplicationContext) { ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) this.context; if (!cwac.isActive()) { // The context has not yet been refreshed -> provide services such as // setting the parent context, setting the application context id, etc if (cwac.getParent() == null) { // The context instance was injected without an explicit parent -> // determine parent for root web application context, if any. ApplicationContext parent = loadParentContext(servletContext); // 设置上下文的父类 cwac.setParent(parent); } // 配置装载和刷新web上下文 configureAndRefreshWebApplicationContext(cwac, 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; } }
从这里可以看出web上下文是存储在servlet上下文中的,key值是
webApplicationContext.ROOT。
开始初始化web程序上下文
if (this.context == null) { this.context = createWebApplicationContext(servletContext); }
跟踪这个方法
org.springframework.web.context.ContextLoader#createWebApplicationContext
创建web应用上下文
protected WebApplicationContext createWebApplicationContext(ServletContext sc) { Class<?> contextClass = determineContextClass(sc); // 如果上下文的类型和ConfigurableWebApplicationContext类型不一致 if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) { throw new ApplicationContextException("Custom context class [" + contextClass.getName() + "] is not of type [" + ConfigurableWebApplicationContext.class.getName() + "]"); } // 返回配置上下文对象 return (ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass); }
返回到这个方法
org.springframework.web.context.ContextLoader#initWebApplicationContext
if (cwac.getParent() == null) {
判断上下文是否是活动的,上下文刷新过一次,还没有关闭。
进入到这个方法,配置装载和刷新web上下文
org.springframework.web.context.ContextLoader#configureAndRefreshWebApplicationContext
// 配置和刷新web上下文 protected void configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac, ServletContext sc) { if (ObjectUtils.identityToString(wac).equals(wac.getId())) { // The application context id is still set to its original default value // -> assign a more useful id based on available information // 从配置中获取上下文的id String idParam = sc.getInitParameter(CONTEXT_ID_PARAM); if (idParam != null) { wac.setId(idParam); } else { // Generate default id... 生成上下文id字符串 webApplicationContext:+servlet.getContextPath() wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + ObjectUtils.getDisplayString(sc.getContextPath())); } } wac.setServletContext(sc); // 获取配置文件路径 contextConfigLocation,配置文件可以是多个的,用,换行分开,可以用占位符 String configLocationParam = sc.getInitParameter(CONFIG_LOCATION_PARAM); if (configLocationParam != null) { wac.setConfigLocation(configLocationParam); } // The wac environment's #initPropertySources will be called in any case when the context // is refreshed; do it eagerly here to ensure servlet property sources are in place for // use in any post-processing or initialization that occurs below prior to #refresh ConfigurableEnvironment env = wac.getEnvironment(); if (env instanceof ConfigurableWebEnvironment) { ((ConfigurableWebEnvironment) env).initPropertySources(sc, null); } // 加载子类实现的上下文 customizeContext(sc, wac); // 上下文刷新 wac.refresh(); }
获取配置文件路径 contextConfigLocation,配置文件可以是多个的,用,换行分开,可以用占位符
String configLocationParam = sc.getInitParameter(CONFIG_LOCATION_PARAM);
// 加载子类实现的上下文 customizeContext(sc, wac);
org.springframework.web.context.ContextLoader#customizeContext 上下文初始化器找到具体要初始化的上下文类进行初始化
protected void customizeContext(ServletContext sc, ConfigurableWebApplicationContext wac) { List<Class<ApplicationContextInitializer<ConfigurableApplicationContext>>> initializerClasses = determineContextInitializerClasses(sc); for (Class<ApplicationContextInitializer<ConfigurableApplicationContext>> initializerClass : initializerClasses) { Class<?> initializerContextClass = GenericTypeResolver.resolveTypeArgument(initializerClass, ApplicationContextInitializer.class); if (initializerContextClass != null && !initializerContextClass.isInstance(wac)) { throw new ApplicationContextException(String.format( "Could not apply context initializer [%s] since its generic parameter [%s] " + "is not assignable from the type of application context used by this " + "context loader: [%s]", initializerClass.getName(), initializerContextClass.getName(), wac.getClass().getName())); } this.contextInitializers.add(BeanUtils.instantiateClass(initializerClass)); } AnnotationAwareOrderComparator.sort(this.contextInitializers); for (ApplicationContextInitializer<ConfigurableApplicationContext> initializer : this.contextInitializers) { initializer.initialize(wac); } }
最后
本次介绍到这里,以上内容仅供参考。
推荐阅读
-
深入理解Spring中的事务传播行为与隔离级别的详细剖析
-
深入理解Spring源码:关键概念剖析
-
深入理解Spring源码(二):探讨Spring框架中应用的设计模式
-
深入理解Spring源码系列(六):剖析循环依赖与三级缓存——详解DefaultSingletonBeanRegistry
-
深入理解Spring源码:揭开循环依赖问题解决之道(第一篇): 基本原理剖析
-
深入理解Spring源码:配置类处理器(ConfigurationClassPostProcessor)详解(三) - @Import注解的处理机制
-
深入剖析Spring框架源码系列之一:解密Spring的架构奥秘
-
逐步深入剖析Spring框架中的IOC核心原理:每次研读都带来新理解与启发
-
深入理解Spring源码:剖析循环依赖问题
-
入门讲解:Spring源代码探索系列" 1. Spring源码剖析导论 2. Spring容器基础操作实战 3. Spring核心容器组件详解 4. 详解Spring基础:XmlBeanFactory源码解析 5. 掌握关键技术:如何获取并处理Document 6. 精读细节:BeanDefinitions的解析与注册过程 7. 深度解析:bean标签在源码中的执行与注册路径