Spring 启动流程源码解析

1. Spring 启动配置

Spring的启动是基于 web 容器的,所有 web工程的初始配置都写在 web.xml 中,该文件一般配置了context 参数,servlet 和监听器(listener)。< context-param >是初始化 Context 的配置,< listener >调用 Spring 包中的 ContextLoaderListener ,用于监听 web 容器初始化事件,并加载相关配置

<!-- Spring 启动配置文件 -->
    <context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring-mybatis.xml</param-value>
	</context-param>
<!-- Spring 启动监听器 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

<!-- Spring MVC servlet -->
	<servlet>
		<servlet-name>SpringMVC</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:spring-mvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
		<async-supported>true</async-supported>
	</servlet>

2. Spring 启动流程

Spring 的启动流程可以分为两部分:

  1. 基于 web 容器的全局域 ServletContext 创建 WebApplicationContext作为 RootContext,也就是整个框架的核心容器
  2. 配置的其他 Spring servlet 基于 RootContext 创建自己的 WebApplicationContext,从而持有自己的 bean 空间

2.1 Spring 基于 ServletContext 创建 RootContext

  1. Spring 的启动其实就是 IoC 容器的启动过程,其核心监听器 ContextLoaderListener 父类是 ContextLoader,实现了 ServletContextListener 接口,在容器启动时会触发其 contextInitialized 初始化方法

    public class ContextLoaderListener extends ContextLoader implements ServletContextListener {
     /**
      * Initialize the root web application context.
      */
     @Override
     public void contextInitialized(ServletContextEvent event) {
     	initWebApplicationContext(event.getServletContext());
     }
    }
    
  2. 此处 initWebApplicationContext() 是 ContextLoader 中的方法, 该方法用于对 整个Spring 框架的ApplicationContext 进行初始化,在这里进入了spring IoC的初始化。
    这个方法主要做了三件事:
    【1】createWebApplicationContext()实际创建 XmlWebApplicationContext 作为 RootContext
    【2】configureAndRefreshWebApplicationContext()加载 Spring 配置文件中的配置并创建 bean
    【3】servletContext.setAttribute()将 WebApplicationContext 放入 ServletContext 全局域

    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!");
       }
       
       ······
       
       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) {
       		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);
       			}
       			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);
       	}
    
       	······
       	
       	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;
       }
    }
    
  3. configureAndRefreshWebApplicationContext(cwac, servletContext)会从 web.xml 中读取 contextConfigLocation 配置,也就是spring xml文件配置,将其存入 WebApplicationContext 中,最后调用refresh() 方法执行所有Java对象的创建工作。

    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
    		String idParam = sc.getInitParameter(CONTEXT_ID_PARAM);
    		if (idParam != null) {
    			wac.setId(idParam);
    		}
    		else {
    			// Generate default id...
    			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(); // Spring IOC 创建 Bean
    }
    
  4. refresh() 方法的实现在 AbstractApplicationContext类中,其主要方法功能如图所示。

    public void refresh() throws BeansException, IllegalStateException {
    	synchronized (this.startupShutdownMonitor) {
    		// Prepare this context for refreshing.
    		prepareRefresh();
    
    		// Tell the subclass to refresh the internal bean factory.
    		ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
    
    		// Prepare the bean factory for use in this context.
    		prepareBeanFactory(beanFactory);
    
    		try {
    			// Allows post-processing of the bean factory in context subclasses.
    			postProcessBeanFactory(beanFactory);
    
    			// Invoke factory processors registered as beans in the context.
    			invokeBeanFactoryPostProcessors(beanFactory);
    
    			// Register bean processors that intercept bean creation.
    			registerBeanPostProcessors(beanFactory);
    
    			// Initialize message source for this context.
    			initMessageSource();
    
    			// Initialize event multicaster for this context.
    			initApplicationEventMulticaster();
    
    			// Initialize other special beans in specific context subclasses.
    			onRefresh();
    
    			// Check for listener beans and register them.
    			registerListeners();
    
    			// Instantiate all remaining (non-lazy-init) singletons.
    			finishBeanFactoryInitialization(beanFactory);
    
    			// Last step: publish corresponding event.
    			finishRefresh();
    		}
    
    		catch (BeansException ex) {
    			if (logger.isWarnEnabled()) {
    				logger.warn("Exception encountered during context initialization - " +
    						"cancelling refresh attempt: " + ex);
    			}
    
    			// Destroy already created singletons to avoid dangling resources.
    			destroyBeans();
    
    			// Reset 'active' flag.
    			cancelRefresh(ex);
    
    			// Propagate exception to caller.
    			throw ex;
    		}
    
    		finally {
    			// Reset common introspection caches in Spring's core, since we
    			// might not ever need metadata for singleton beans anymore...
    			resetCommonCaches();
    		}
    	}
    }
    

在这里插入图片描述

2.2 Spring servlet 基于 RootContext 创建 WebApplicationContext

  1. contextLoaderListener 监听器初始化完毕后,开始初始化web.xml中配置的 servlet。servlet可以配置多个,以最常见的DispatcherServlet为例,DispatcherServlet 在初始化的时候会建立自己的 IoC context,用以持有spring mvc相关的bean。DispatcherServlet 继承关系如下图,web容器启动时 servlet 的调用链如下:
GenericServlet#init()->HttpServletBean#init()->FrameworkServlet#initServletBean()->initWebApplicationContext()

此处 FrameworkServlet # initWebApplicationContext() 方法与 Spring框架创建 RootContext 流程大致相同,分为以下几步,只不过设置的 parent context 不是 ServletConext 而是 Spring 核心容器 RootContext
【1】WebApplicationContextUtils.getWebApplicationContext获取 RootContext
【2】使用已有或者新建的 WebApplicationContext 加载 SpringMVC 配置文件中的配置并创建 bean
【3】onRefresh()调用 DispatcherServlet#initStrategies() 进行 servlet 初始化
【4】将 servlet 自己的 WebApplicationContext 存入 ServletContext

protected WebApplicationContext initWebApplicationContext() {
		// 获取 root context
		WebApplicationContext rootContext =
				WebApplicationContextUtils.getWebApplicationContext(getServletContext());
		WebApplicationContext wac = null;

		if (this.webApplicationContext != null) {
			// A context instance was injected at construction time -> use it
			wac = this.webApplicationContext;
			if (wac instanceof ConfigurableWebApplicationContext) {
				ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) wac;
				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 -> set
						// the root application context (if any; may be null) as the parent
						cwac.setParent(rootContext);
					}
					configureAndRefreshWebApplicationContext(cwac);
				}
			}
		}
		if (wac == null) {
			// No context instance was injected at construction time -> see if one
			// has been registered in the servlet context. If one exists, it is assumed
			// that the parent context (if any) has already been set and that the
			// user has performed any initialization such as setting the context id
			wac = findWebApplicationContext();
		}
		if (wac == null) {
			// No context instance is defined for this servlet -> create a local one
			wac = createWebApplicationContext(rootContext);
		}

		if (!this.refreshEventReceived) {
			// Either the context is not a ConfigurableApplicationContext with refresh
			// support or the context injected at construction time had already been
			// refreshed -> trigger initial onRefresh manually here.
			onRefresh(wac);
		}

		if (this.publishContext) {
			// Publish the context as a servlet context attribute.
			String attrName = getServletContextAttributeName();
			getServletContext().setAttribute(attrName, wac);
			if (this.logger.isDebugEnabled()) {
				this.logger.debug("Published WebApplicationContext of servlet '" + getServletName() +
						"' as ServletContext attribute with name [" + attrName + "]");
			}
		}

		return wac;
	}

在这里插入图片描述

3. 总结

  1. 一个web应用部署在 web 容器中,web容器为其提供一个全局域 ServletContext,作为spring IoC容器 WebApplicationContext 的宿主环境

  2. 在web.xml 中配置的 contextLoaderListener 会在容器启动时初始化 一个WebApplicationContext 作为 RootContext。这是一个接口类,其实际的实现类是 XmlWebApplicationContext (在 ContextLoader# determineContextClass()方法中决定)。这个就是 spring 的核心 IoC 容器,其对应 bean 定义的配置由web.xml 中的 context-param 标签指定,并通过 refresh()方法完成 bean 创建。IoC容器初始化完毕后,Spring 将以 WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE作为 Key,将创建的 XmlWebApplicationContext 对象存储到 ServletContext 中,便于之后作为 RootContext 使用

    protected Class<?> determineContextClass(ServletContext servletContext) {
        // 如果在 web.xml 中直接指定了 ContextClass
     	String contextClassName = servletContext.getInitParameter(CONTEXT_CLASS_PARAM);
     	if (contextClassName != null) {
     		try {
     			return ClassUtils.forName(contextClassName, ClassUtils.getDefaultClassLoader());
     		}
     		catch (ClassNotFoundException ex) {
     			throw new ApplicationContextException(
     					"Failed to load custom context class [" + contextClassName + "]", ex);
     		}
     	}
     	else { 
     	// 没有直接指定,则读取属性文件 ContextLoader.properties 的配置
     	/** org.springframework.web.context.WebApplicationContext=
         org.springframework.web.context.support.XmlWebApplicationContext
         */
     		contextClassName = defaultStrategies.getProperty(WebApplicationContext.class.getName());
     		try {
     			return ClassUtils.forName(contextClassName, ContextLoader.class.getClassLoader());
     		}
     		catch (ClassNotFoundException ex) {
     			throw new ApplicationContextException(
     					"Failed to load default context class [" + contextClassName + "]", ex);
     		}
     	}
     }
    
  3. contextLoaderListener 监听器初始化完毕后,开始初始化 web.xml 中配置的servlet。以DispatcherServlet为例,DispatcherServlet 在初始化的时候会建立自己的 context,用以持有spring mvc相关的 bean,并完成 bean 的创建。初始化时设置其 parent context 为 Spring 的核心容器 RootContext,这样每个 servlet 都拥有自己的 context,即拥有自己独立的bean空间,同时又共享 RootContext 中定义的那些bean。当 Spring 组件在执行 getBean 时,如果在自己的 context 中找不到对应的bean,则会在父ApplicationContext (通常为Spring 核心容器 RootContext)中去找,这也就解释了在 DispatcherServlet 中为何可以获取到由 ContextLoaderListener 创建的ApplicationContext中的bean。

在这里插入图片描述

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我会尽力解释 Spring 容器启动流程源码。 1. 加载配置文件 Spring 容器启动时,首先会读取配置文件,通常是 `applicationContext.xml`。配置文件中定义了需要被 Spring 管理的 bean,以及它们的依赖关系等信息。 2. 创建 BeanDefinition Spring 容器解析配置文件,创建对应的 BeanDefinition 对象。BeanDefinition 定义了 bean 的基本信息,如 bean 的类名、scope、依赖关系等等。 3. 注册 BeanDefinition 接下来,Spring 容器将创建的 BeanDefinition 注册到 BeanDefinitionRegistry 中。BeanDefinitionRegistry 是一个接口,它定义了注册、查询、删除 BeanDefinition 的方法。 4. 实例化 Bean 接下来,Spring 容器将开始实例化 bean。Spring 容器使用反射创建 bean 的实例,然后根据配置文件中的信息对 bean 进行初始化。 5. 填充属性值 在 bean 实例化之后,Spring 容器会开始填充属性值。Spring 容器会根据配置文件中的信息,自动为 bean 填充属性值。这些属性可以是基本类型、引用类型、集合类型等等。 6. 调用 BeanPostProcessor 在填充完属性值之后,Spring 容器会调用 BeanPostProcessor 的方法。BeanPostProcessor 是一个接口,它定义了在 bean 实例化和初始化过程中的回调方法。通过实现 BeanPostProcessor,我们可以在 bean 实例化和初始化的过程中做一些自定义的操作。 7. 初始化 Bean 在填充完属性值和调用 BeanPostProcessor 之后,Spring 容器会调用 bean 的初始化方法。初始化方法可以是 init-method 指定的方法,也可以是实现了 InitializingBean 接口的 afterPropertiesSet() 方法。 8. 注册销毁方法 当 bean 的生命周期结束时,Spring 容器会调用它的销毁方法。销毁方法可以是 destroy-method 指定的方法,也可以是实现了 DisposableBean 接口的 destroy() 方法。 以上就是 Spring 容器启动流程的大概过程。其中,BeanDefinition、BeanPostProcessor、InitializingBean、DisposableBean 等接口和类都是 Spring 框架中提供的,它们为我们提供了更加灵活的配置和扩展方式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值