spring源碼解析bean初始化後的實現和其餘入口

前言java

本文轉自「天河聊技術」微信公衆號web

本次主要介紹bean初始化、依賴注入後續的實現和其餘入口spring

 

正文緩存

找到這個方法微信

org.springframework.context.support.AbstractApplicationContext#refreshapp

這一行ide

finishRefresh();

進入this

org.springframework.context.support.AbstractApplicationContext#finishRefreshdebug

protected void finishRefresh() {
   // Clear context-level resource caches (such as ASM metadata from scanning).清除上下文級別的資源緩存(例如掃描的ASM元數據)。
   clearResourceCaches();

   // Initialize lifecycle processor for this context.爲這個上下文初始化生命週期處理器。
   initLifecycleProcessor();

   // Propagate refresh to lifecycle processor first.首先傳播刷新到生命週期處理器。
   getLifecycleProcessor().onRefresh();

   // Publish the final event.發佈最後的事件。
   publishEvent(new ContextRefreshedEvent(this));

   // Participate in LiveBeansView MBean, if active.
   LiveBeansView.registerApplicationContext(this);
}

這一行生命週期

initLifecycleProcessor();

初始化生命週期處理器

進入

org.springframework.context.support.AbstractApplicationContext#initLifecycleProcessor初始化LifecycleProcessor。若是上下文沒有定義,則使用DefaultLifecycleProcessor

protected void initLifecycleProcessor() {
   ConfigurableListableBeanFactory beanFactory = getBeanFactory();
   if (beanFactory.containsLocalBean(LIFECYCLE_PROCESSOR_BEAN_NAME)) {
      this.lifecycleProcessor =
            beanFactory.getBean(LIFECYCLE_PROCESSOR_BEAN_NAME, LifecycleProcessor.class);
      if (logger.isDebugEnabled()) {
         logger.debug("Using LifecycleProcessor [" + this.lifecycleProcessor + "]");
      }
   }
   else {
      DefaultLifecycleProcessor defaultProcessor = new DefaultLifecycleProcessor();
      defaultProcessor.setBeanFactory(beanFactory);
      this.lifecycleProcessor = defaultProcessor;
      beanFactory.registerSingleton(LIFECYCLE_PROCESSOR_BEAN_NAME, this.lifecycleProcessor);
      if (logger.isDebugEnabled()) {
         logger.debug("Unable to locate LifecycleProcessor with name '" +
               LIFECYCLE_PROCESSOR_BEAN_NAME +
               "': using default [" + this.lifecycleProcessor + "]");
      }
   }
}

回到這個方法

org.springframework.context.support.AbstractApplicationContext#refresh

這一行

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;
}

異常處理

返回

org.springframework.web.context.ContextLoader#initWebApplicationContext這一行

//        把spring上下文放在servlet上下文上
         servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);

返回

@Override
public void contextInitialized(ServletContextEvent event) {
   initWebApplicationContext(event.getServletContext());
}

 

我跟蹤的順序是跟着servlet容器啓動這個入口看來跟蹤的,還有兩個入口給你們介紹下

ClassPathXmlApplicationContext

這個方法

org.springframework.context.support.ClassPathXmlApplicationContext#ClassPathXmlApplicationContext(java.lang.String)

public ClassPathXmlApplicationContext(String configLocation) throws BeansException {
   this(new String[] {configLocation}, true, null);
}

參數是傳入一個bean定義配置文件

進入org.springframework.context.support.ClassPathXmlApplicationContext#ClassPathXmlApplicationContext(java.lang.String[], boolean, org.springframework.context.ApplicationContext)這個方法

public ClassPathXmlApplicationContext(
      String[] configLocations, boolean refresh, @Nullable ApplicationContext parent)
      throws BeansException {

   super(parent);
   setConfigLocations(configLocations);
   if (refresh) {
      refresh();
   }
}

進入org.springframework.context.support.AbstractApplicationContext#refresh這個方法,剩下的以往的文章中都介紹過了

 

FileSystemXmlApplicationContext

找到org.springframework.context.support.FileSystemXmlApplicationContext#FileSystemXmlApplicationContext(java.lang.String)這個方法

public FileSystemXmlApplicationContext(String configLocation) throws BeansException {
   this(new String[] {configLocation}, true, null);
}

參數也是傳入一個bean定義配置文件

進入

org.springframework.context.support.FileSystemXmlApplicationContext#FileSystemXmlApplicationContext(java.lang.String[], boolean, org.springframework.context.ApplicationContext)

public FileSystemXmlApplicationContext(
      String[] configLocations, boolean refresh, @Nullable ApplicationContext parent)
      throws BeansException {

   super(parent);
   setConfigLocations(configLocations);
   if (refresh) {
      refresh();
   }
}

進入org.springframework.context.support.AbstractApplicationContext#refresh這個方法,剩下的以往的文章都介紹過了,applicationContext的生涯新實現就解析完了

 

最後

 

本次介紹到這裏,以上內容僅供參考。

相關文章
相關標籤/搜索