Spring源码系列:Spring的启动过程 ・ glmapper

匿名 (未验证) 提交于 2019-12-03 00:15:02

Spring对于程序员说来说都不陌生;作为一个强大的开源技术,帮助我们能够更好的进行项目的开发与维护。
直接进入主题吧。Spring的启动过程实际上就是Ioc容器初始化以及载入Bean的过程;本文主要是学习记录下前半部分(Ioc容器的初始化),新手上路,如有错误,请指正!
1.从配置文件说起

12345678
<listener>       <listener-class>org.springframework.web.context.ContextLoaderListener     </listener-class>  </listener> <context-param>      <param-name>contextConfigLocation</param-name>      <param-value>classpath:applicationContext.xml</param-value>  </context-param>

在一般的WEB项目中,项目的启动一般是从web.xml文件的载入开始的。如果我们的项目中使用了Spring,那么你肯定会在你的web.xml文件中看到上面的配置。Spring正是通过ContextLoaderListener监听器来进行容器初始化的。下面通过代码进行分析。

2.Spring容器加载的三步走

  • step1:创建一个WebApplicationContext
  • step2:配置并且刷新Bean
  • step3:将容器初始化到servlet上下文中

3.WebApplicationContext的创建过程

1
public class  extends ContextLoader implements ServletContextListener

从ContextLoaderListener的定义可以看出,该监听器继承了ContextLoader,并且重写了ServletContextListener中的contextInitialized和contextDestroyed方法。

在contextInitialized中,通过调用父类(ContextLoader)的initWebApplicationContext方法进行容器创建:

1234
public void contextInitialized(ServletContextEvent event) {    initWebApplicationContext(event.getServletContext());}

下面来看initWebApplicationContext的代码:

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374

下面我们在看下是如何创建WebApplicationContext的

12345678910
protected WebApplicationContext createWebApplicationContext(ServletContext sc) {    //首先来确定context是由什么类定义的,并且判断当前容器是否为可配置的    Class<?> contextClass = determineContextClass(sc);    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);  }

最后来看下determineContextClass这个方法

12345678910111213141516171819202122232425
protected Class<?> determineContextClass(ServletContext servletContext) {    //首先从web.xml中查看用户是否自己定义了context    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);      }    }    /*如果没有,则去defaultStrategies里面取【defaultStrategies是Propertites类的/对象,在ContextLoader中的静态代码块中初始化的;具体可看下下面的图像】;默认容器是XmlWebApplicationContext*/  else {   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);      }    }  }

总的来说就是:Spring的web工程首先回去检查用户是否自己定义了context,如果有就采用;如果没有就使用Spring默认的。
defaultStrategies初始化:

至此,容器创建完成。下面是整个过程的一个流程图(有疏漏,回头补一个时序图):

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!