spring加载bean原理

来源:互联网 发布:java一年多的工作经验 编辑:程序博客网 时间:2024/06/05 23:31

Spring管理bean:

1.从web.xml里面配置的ContextLoaderListener开始。

2.ContextLoaderListener继承ContextLoader

3.执行ContextLoaderListener的contextInitialized方法,获得servletContext.

4.把servletContext传入ContextLoader的initWebApplicationContext(ServletContext servletContext)方法并执行。

5.通过ContextLoader.createWebApplicationContext(servletContext)获得this.contextWebApplicationContext实例。

默认情况下这个context就是XmlWebApplicationContext。

这个XmlWebApplicationContext继承一个AbstractApplicationContext类。

再执行ContextLoader.configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac, ServletContext sc)方法,

执行AbstractApplicationContext的.refresh();方法

[java] view plain copy
  1. @Override  
  2. public void refresh() throws BeansException, IllegalStateException {  
  3.     synchronized (this.startupShutdownMonitor) {  
  4.         // Prepare this context for refreshing.  
  5.         prepareRefresh();  
  6.   
  7.         // Tell the subclass to refresh the internal bean factory.  
  8.         ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();  
  9.   
  10.         // Prepare the bean factory for use in this context.  
  11.         prepareBeanFactory(beanFactory);  
  12.   
  13.         try {  
  14.             // Allows post-processing of the bean factory in context subclasses.  
  15.             postProcessBeanFactory(beanFactory);  
  16.   
  17.             // Invoke factory processors registered as beans in the context.  
  18.             invokeBeanFactoryPostProcessors(beanFactory);  
  19.   
  20.             // Register bean processors that intercept bean creation.  
  21.             registerBeanPostProcessors(beanFactory);  
  22.   
  23.             // Initialize message source for this context.  
  24.             initMessageSource();  
  25.   
  26.             // Initialize event multicaster for this context.  
  27.             initApplicationEventMulticaster();  
  28.   
  29.             // Initialize other special beans in specific context subclasses.  
  30.             onRefresh();  
  31.   
  32.             // Check for listener beans and register them.  
  33.             registerListeners();  
  34.   
  35.             // Instantiate all remaining (non-lazy-init) singletons.  
  36.             finishBeanFactoryInitialization(beanFactory);  
  37.   
  38.             // Last step: publish corresponding event.  
  39.             finishRefresh();  
  40.         }  
  41.   
  42.         catch (BeansException ex) {  
  43.             logger.warn("Exception encountered during context initialization - cancelling refresh attempt", ex);  
  44.   
  45.             // Destroy already created singletons to avoid dangling resources.  
  46.             destroyBeans();  
  47.   
  48.             // Reset 'active' flag.  
  49.             cancelRefresh(ex);  
  50.   
  51.             // Propagate exception to caller.  
  52.             throw ex;  
  53.         }  
  54.     }  
这个方法完成了WebApplicationContext里面的beanfactory的初始化和bean载入,beanfactorypostprocessor的调用,beanpostprocessor的注册,ApplicationEvent的监听和注册,non-lazy-init的bean的初始化。

换言之,已经把该准备的都准备好了,只需要有请求来获取bean,就根据情况或返回已经初始化的bean或进行bean的Instantiation 和 Initialization。


原创粉丝点击