【spring version : 4.1.6.RELEASE】web
使用spring的項目中,通常都會在web.xml中配置ContextLoaderListener,它就是spring ioc 的入口spring
<!-- spring context listener --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
入口:ContextLoaderListener.contextInitialized(ServletContextEvent)mybatis
1。首先,建立一個上下文 WebApplicationContext context。使用默認的策略: XmlWebApplicationContext 的實例做爲上下文。
2. 將 XmlWebApplicationContext 強轉成 ConfigurableWebApplicationContext ,設置 parent (是null)
2. 調用 ContextLoader.configureAndRefreshWebApplicationContext 刷新上下文app
ContextLoader.configureAndRefreshWebApplicationContext 刷新上下文:
1. 將ServletContext設置到 WebApplicationContext
獲取web.xml中配置的contextConfigLocation,設置到 WebApplicationContext 中
2. wac.refresh() 刷新上下文post
刷新上下文:
AbstractApplicationContext.refresh() 做以下工做:
// Prepare this context for refreshing.
【1】 prepareRefresh();this
// Tell the subclass to refresh the internal bean factory.
【2】 ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
1. 建立一個 DefaultListableBeanFactory 的實例。
2. 加載bean的定義 loadBeanDefinitions(DefaultListableBeanFactory)
(BeanDefinitionParser)spa
// Prepare the bean factory for use in this context.
【3】 prepareBeanFactory(beanFactory);
1. 設置beanFactory的classLoader
2. 設置 beanPostProcesser.
beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
3. 註冊spring內部指定的依賴類型和相應的自動注入值。代理
// Register a special dependency type with corresponding autowired value. beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory); beanFactory.registerResolvableDependency(ResourceLoader.class, this); beanFactory.registerResolvableDependency(ApplicationEventPublisher.class, this); beanFactory.registerResolvableDependency(ApplicationContext.class, this);
因此,咱們能夠經過autowired的方式拿到ApplicationContext的實例code
4. Register default environment beans.xml
// Allows post-processing of the bean factory in context subclasses.
【4】 postProcessBeanFactory(beanFactory);
beanFactory 後置處理,留給子類擴展,處理 beanFactory ,作相似【3】中的 prepareBeanFactory() 的事情,好比能夠在這裏添加 beanPostProcesser。
context 的實例是 XmlWebApplicationContext,這個方法最後會調 AbstractRefreshableWebApplicationContext.postProcessBeanFactory(ConfigurableListableBeanFactory)
protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) { // 添加ServletContextAwareProcessor,用來處理實現了ServletContextAware接口的bean beanFactory.addBeanPostProcessor(new ServletContextAwareProcessor(this.servletContext, this.servletConfig)); beanFactory.ignoreDependencyInterface(ServletContextAware.class); beanFactory.ignoreDependencyInterface(ServletConfigAware.class); WebApplicationContextUtils.registerWebApplicationScopes(beanFactory, this.servletContext); WebApplicationContextUtils.registerEnvironmentBeans(beanFactory, this.servletContext, this.servletConfig); }
WebApplicationContextUtils.registerWebApplicationScopes(beanFactory, this.servletContext) ---> 會去註冊以下幾個bean,因此在web項目中,咱們能夠直接使用@Resource來依賴注入這些bean
beanFactory.registerResolvableDependency(ServletRequest.class, new RequestObjectFactory()); beanFactory.registerResolvableDependency(ServletResponse.class, new ResponseObjectFactory()); beanFactory.registerResolvableDependency(HttpSession.class, new SessionObjectFactory()); beanFactory.registerResolvableDependency(WebRequest.class, new WebRequestObjectFactory());
// Invoke factory processors registered as beans in the context.
【5】 invokeBeanFactoryPostProcessors(beanFactory);
執行beanFactory的postProcessor。(注意與beanPostProcessor區別開)
裏面會去找到全部的 BeanDefinitionRegistryPostProcessor 去執行 postProcessBeanDefinitionRegistry()。
好比:ConfigurationClassPostProcessor、org.mybatis.spring.mapper.MapperScannerConfigurer
// Register bean processors that intercept bean creation.
【6】 registerBeanPostProcessors(beanFactory);
註冊beanPostProcessor,按順序註冊
// Initialize message source for this context.
【7】 initMessageSource();
// Initialize event multicaster for this context.
【8】 initApplicationEventMulticaster();
初始化事件廣播
// Initialize other special beans in specific context subclasses.
【9】 onRefresh();
// Check for listener beans and register them.
【10】 registerListeners();
// 註冊事件監聽器,事件由ApplicationEventMulticaster去廣播
// Instantiate all remaining (non-lazy-init) singletons.
【11】 finishBeanFactoryInitialization(beanFactory);
解析 bean 的依賴,完成 bean 實例的初始化。
1. 先建立 bean 的 proxy 。
AbstractAutowireCapableBeanFactory.createBean(final String beanName, final RootBeanDefinition mbd, final Object[] args) ..... // Give BeanPostProcessors a chance to return a proxy instead of the target bean instance. // 【分析】:它會調用全部的 BeanPostProcessors 來對 bean 進行處理postProcessBeforeInitialization、postProcessAfterInitialization // 若是要生成代理對象的話,可想而知,BeanFactory在註冊BeanDefinition時確定註冊了一個和生成代理相關的 BeanPostProcessor // 而咱們在spring配置文件中配置了 <aop:aspectj-autoproxy proxy-target-class="true" /> ,它確定會對應一個 BeanDefinitionParser 來解析這個標籤。 // 由此,咱們又找到了 AspectJAutoProxyBeanDefinitionParser.parse(),這個方法就對 AnnotationAwareAspectJAutoProxyCreator 進行了註冊,它間接實現了 BeanPostProcessor Object bean = resolveBeforeInstantiation(beanName, mbd); if (bean != null) { return bean; } ......
2. 若是建立出來了的話,就返回bean的代理,不然就建立非代理的 bean
裏面會決定要不要建立 bean 的 proxy。(AbstractAutoProxyCreator.createProxy)
// Last step: publish corresponding event.
//Finish the refresh of this context, invoking the LifecycleProcessor's onRefresh() method and publishing the ContextRefreshedEvent.
【12】 finishRefresh();