spring源碼是一個很有意思的東西,沒事可以去讀一下,看一下
從最簡單的spring源碼開始解讀,帶web.xml配置的那種,在web.xml中有兩個很重要的配置,
第一個,spring的listener,第二個就是springmvc的最重要的容器了。
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
spring容器的啓動,就在ContextLoaderListener,源碼分析可以從這裏跟進去,找到裏面的initWebApplicationContext方法,spring容器初始化就在這裏。initWebApplicationContext在父類ContextLoader裏,朝下繼續,configureAndRefreshWebApplicationContext方法裏refresh方法,就到了
AbstractApplicationContext
這就是spring最重要的類了,容器初始化的核心流程
/*
* 該方法是spring容器初始化的核心方法。是spring容器初始化的核心流程,是一個典型的父類模板設計模式的運用
* 根據不同的上下文對象,會掉到不同的上下文對象子類方法中
*
* 核心上下文子類有:
* ClassPathXmlApplicationContext
* FileSystemXmlApplicationContext
* AnnotationConfigApplicationContext
* EmbeddedWebApplicationContext(springboot)
*
* */
@Override
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {//加鎖,保證容器創建過程中不會被銷燬
//爲容器初始化做準備,記錄時間,監聽之類的
// Prepare this context for refreshing.
prepareRefresh();
/*
1、創建BeanFactory對象
* 2、xml解析
* 傳統標籤解析:bean、import等
* 自定義標籤解析
* 自定義標籤解析流程:
* a、根據當前解析標籤的頭信息找到對應的namespaceUri
* b、加載spring所以jar中的spring.handlers文件。並建立映射關係
* c、根據namespaceUri從映射關係中找到對應的實現了NamespaceHandler接口的類
* d、調用類的init方法,init方法是註冊了各種自定義標籤的解析類
* e、根據namespaceUri找到對應的解析類,然後調用paser方法完成標籤解析
*
* 3、把解析出來的xml標籤封裝成BeanDefinition對象
* */
// 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.
//這裏是準備完成後正式初始化初始化bean
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();
}
}
}