Spring源码阅读-spring启动
web.xml
web.xml中的spring容器配置
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
listener配置
ContextLoaderListener是spring核心功能启动器,该监听器会监听Servlet容器启动事件,一旦监听到该事件就拉起spring整个容器的启动。
ContextLoaderListner
ContextLoaderListener继承自ContextLoader,关键还实现了javax.servlet.ServletContextListener接口,该接口的contextInitialized方法用来对扩展的容器进行初始化,ContextLoaderListener在该方法中对spring上下文进行初始化, 在初始化容器时需要判断容器是否已经初始化过,如果没有则创建一个新的spring容器,ContextLoader会首先从ServletContext中获取配置的容器类(参数名:contextClass),如果获取不到则使用ContextLoader同一classPath下的ContextLoader.properties中配置的org.springframework.web.context.WebApplicationContext对应的值作为容器类并实例化容器作为spring的根容器。其配置如下:
spring默认容器上下文实现XmlWebApplicationContext
实例化XmlWebApplicationContext并配置好后将容器实例缓存到ServletContext中,因此在web服务中只要能获取到ServletContext就可以根据WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE获取spring根容器,该key固定为:org.springframework.web.context.WebApplicationContext.ROOT
根容器创建
当XmlWebApplicationContext实例化成功后,ContextLoader会调用configureAndRefreshWebApplicationContext对容器上下文进行配置和初始化,其中就包括Spring基本属性设置、配置文件加载,解析,对象实例化等动作。Spring容器上下文继承关系如下:
XmlWebApplicationContext的配置文件加载,解析,对象实例化等动作都统一在AbstractApplicationContext.refresh方法中完成,refresh结束后,容器会发布一个ContextRefreshedEvent事件,供依赖容器启动结束的组件或者第三方定制扩展使用(例如Dubbo,通过监听该事件触发其配置加载后的实例包装动作)。具体的刷新核心代码我们看一下:
1 // Prepare this context for refreshing. 2 prepareRefresh(); 3 4 // Tell the subclass to refresh the internal bean factory. 5 ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory(); 6 7 // Prepare the bean factory for use in this context. 8 prepareBeanFactory(beanFactory); 9 10 try { 11 // Allows post-processing of the bean factory in context subclasses. 12 postProcessBeanFactory(beanFactory); 13 14 // Invoke factory processors registered as beans in the context. 15 invokeBeanFactoryPostProcessors(beanFactory); 16 17 // Register bean processors that intercept bean creation. 18 registerBeanPostProcessors(beanFactory); 19 20 // Initialize message source for this context. 21 initMessageSource(); 22 23 // Initialize event multicaster for this context. 24 initApplicationEventMulticaster(); 25 26 // Initialize other special beans in specific context subclasses. 27 onRefresh(); 28 29 // Check for listener beans and register them. 30 registerListeners(); 31 32 // Instantiate all remaining (non-lazy-init) singletons. 33 finishBeanFactoryInitialization(beanFactory); 34 35 // Last step: publish corresponding event. 36 finishRefresh(); 37 }
View Code
BeanFactory
容器刷新涉及最终要的部件是ApplicationContext辅助类以及beanFactory,ApplicationContext负责spring配置文件的加载和解析,将bean配置解析转换成一个个BeanDefinition并传递给bean工厂,bean工厂调度ApplicationContext辅助类的解析工作,并在解析完成后负责bean对象实例化。
obtainFreshBeanFactory方法完成工厂创建和刷新,工厂刷新工作内容主要是调度ApplicationContex将bean配置解析转换成一个个BeanDefinition,具体的配置解析和加载工作实现在不同的ApplicationContext实现类,例如xml解析在XmlWebApplicationContext中定义。
通过调用父类org.springframework.web.context.support.XmlWebApplicationContext的loadBeanDefinitions实际完成加载工作,其实所有ApplicationContext的对应的配置加载实现都在org.springframework.web.context.support包下面:
org.springframework.beans.factory.xml.DefaultNamespaceHandlerResolver负责加载spring配置文件解析器信息配置
扩展NameSpaceHandler
其中配置了配置文件命名空间和NamespaceHandler类的映射关系。
NamespaceHandler中定义了不同类型xml节点的解析器。容器启动过程中XmlWebApplicationContext会通过XmlBeanDefinitionReader读取Spring的配置文件,在解析每一个配置节点时,会从NamespaceHandler中获取对应的解析器进行解析并转换成Spring容器中的实例或者spring配置对象。