1.org.springframework.web.context.ContextLoaderListener 一个ServletContextListener,web容器启动监听器
1.1内有成员ContextLoader,当contextInitialized时,调用this.contextLoader.initWebApplicationContext(event.getServletContext());
2.org.springframework.web.context.ContextLoader.createWebApplicationContext(ServletContext, ApplicationContext) 创建ApplicationContext
在此方法中调用org.springframework.context.support.AbstractApplicationContext.refresh()
3.在refresh方法里面org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(),创建BeanFactory对象,具体方法在org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory()
4.创建BeanFactory之后加载BeanDefinitions org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(DefaultListableBeanFactory)
注意,beanfactory同时也是BeanDefinitionRegistry,在读取到bean后调用org.springframework.beans.factory.support.DefaultListableBeanFactory.registerBeanDefinition(String, BeanDefinition)将bean定义放到beanDefinitionMap缓存,
ApplicationContext同时也是resourceloader,org.springframework.context.support.GenericApplicationContext.getResources(String)读取资源
5. fresh后面,当beanfactory初始化完之后,调用org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(ConfigurableListableBeanFactory)单例bean的实例化过程,单例bean实例保存在singletonObjects中。
6.获取bean,如果没有就创建org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(String, Class<T>, Object[], boolean),如果是工厂bean,先创建工厂bean,然后从工厂bean创建对象,单例的工厂bean存储在factoryBeanObjectCache中,注意,单例不仅是isSingleton返回true而且bean本身必须是单例。
7.创建bean, org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(String, RootBeanDefinition, Object[]),其中,调用org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(String, RootBeanDefinition, Object[])
8.BeanWrapper里面包含bean实例等信息,bean属性注入也在这里org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(String, AbstractBeanDefinition, BeanWrapper):
9.bean实例的后处理org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(String, Object, RootBeanDefinition)
其中,org.springframework.context.support.ApplicationContextAwareProcessor后处理在springMVC中尤为关键,因为org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping实现了ApplicationContextAwareProcessor这一接口,这个类就是解析所有requestMapping注解,获得并缓存请求url与处理类关系的。