Spring 啓動記錄(3)

Spring 啓動記錄(3)

一、當AbstractApplicationContext出事完成後是spring

AbstractRefreshableApplicationContext的初始化工做app

該類負責每次父類調用refresh的調用的ide

protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {
   refreshBeanFactory();
   ConfigurableListableBeanFactory beanFactory = getBeanFactory();
   if (logger.isDebugEnabled()) {
      logger.debug("Bean factory for " + getDisplayName() + ": " + beanFactory);
   }
   return beanFactory;
}
@Override
public abstract ConfigurableListableBeanFactory getBeanFactory() throws IllegalStateException;

 

其中這個getBeanFactory是抽象方法它的子類即AbstractRefreshableApplicationContext實現this

其中每次的刷新都會建立一個新的ApplicationContext的實現類:DefaultListableBeanFactoryspa

protected final void refreshBeanFactory() throws BeansException {
   if (hasBeanFactory()) {
      destroyBeans();
      closeBeanFactory();
   }
   try {
      DefaultListableBeanFactory beanFactory = createBeanFactory();
      beanFactory.setSerializationId(getId());
      customizeBeanFactory(beanFactory);
      loadBeanDefinitions(beanFactory);
      synchronized (this.beanFactoryMonitor) {
         this.beanFactory = beanFactory;
      }
   }
   catch (IOException ex) {
      throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex);
   }
}
protected DefaultListableBeanFactory createBeanFactory() {
   return new DefaultListableBeanFactory(getInternalParentBeanFactory());
}

 

 

public abstract class AbstractRefreshableApplicationContext extends AbstractApplicationContext {

   private Boolean allowBeanDefinitionOverriding;

   private Boolean allowCircularReferences;

   /** Bean factory for this context */
   private DefaultListableBeanFactory beanFactory;

   /** Synchronization monitor for the internal BeanFactory */
   private final Object beanFactoryMonitor = new Object();


   /**
    * Create a new AbstractRefreshableApplicationContext with no parent.
    */
   public AbstractRefreshableApplicationContext() {
   }

   /**
    * Create a new AbstractRefreshableApplicationContext with the given parent context.
    * @param parent the parent context
    */
   public AbstractRefreshableApplicationContext(ApplicationContext parent) {
      super(parent);
   }


   /**
    * Set whether it should be allowed to override bean definitions by registering
    * a different definition with the same name, automatically replacing the former.
    * If not, an exception will be thrown. Default is "true".
    * @see org.springframework.beans.factory.support.DefaultListableBeanFactory#setAllowBeanDefinitionOverriding
    */
   public void setAllowBeanDefinitionOverriding(boolean allowBeanDefinitionOverriding) {
      this.allowBeanDefinitionOverriding = allowBeanDefinitionOverriding;
   }

   /**
    * Set whether to allow circular references between beans - and automatically
    * try to resolve them.
    * <p>Default is "true". Turn this off to throw an exception when encountering
    * a circular reference, disallowing them completely.
    * @see org.springframework.beans.factory.support.DefaultListableBeanFactory#setAllowCircularReferences
    */
   public void setAllowCircularReferences(boolean allowCircularReferences) {
      this.allowCircularReferences = allowCircularReferences;
   }


   /**
    * This implementation performs an actual refresh of this context's underlying
    * bean factory, shutting down the previous bean factory (if any) and
    * initializing a fresh bean factory for the next phase of the context's lifecycle.
    */
   @Override
   protected final void refreshBeanFactory() throws BeansException {
      if (hasBeanFactory()) {
         destroyBeans();
         closeBeanFactory();
      }
      try {
         DefaultListableBeanFactory beanFactory = createBeanFactory();
         beanFactory.setSerializationId(getId());
         customizeBeanFactory(beanFactory);
         loadBeanDefinitions(beanFactory);
         synchronized (this.beanFactoryMonitor) {
            this.beanFactory = beanFactory;
         }
      }
      catch (IOException ex) {
         throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex);
      }
   }

   @Override
   protected void cancelRefresh(BeansException ex) {
      synchronized (this.beanFactoryMonitor) {
         if (this.beanFactory != null)
            this.beanFactory.setSerializationId(null);
      }
      super.cancelRefresh(ex);
   }

   @Override
   protected final void closeBeanFactory() {
      synchronized (this.beanFactoryMonitor) {
         this.beanFactory.setSerializationId(null);
         this.beanFactory = null;
      }
   }

   /**
    * Determine whether this context currently holds a bean factory,
    * i.e. has been refreshed at least once and not been closed yet.
    */
   protected final boolean hasBeanFactory() {
      synchronized (this.beanFactoryMonitor) {
         return (this.beanFactory != null);
      }
   }

   @Override
   public final ConfigurableListableBeanFactory getBeanFactory() {
      synchronized (this.beanFactoryMonitor) {
         if (this.beanFactory == null) {
            throw new IllegalStateException("BeanFactory not initialized or already closed - " +
                  "call 'refresh' before accessing beans via the ApplicationContext");
         }
         return this.beanFactory;
      }
   }

   /**
    * Overridden to turn it into a no-op: With AbstractRefreshableApplicationContext,
    * {@link #getBeanFactory()} serves a strong assertion for an active context anyway.
    */
   @Override
   protected void assertBeanFactoryActive() {
   }

   /**
    * Create an internal bean factory for this context.
    * Called for each {@link #refresh()} attempt.
    * <p>The default implementation creates a
    * {@link org.springframework.beans.factory.support.DefaultListableBeanFactory}
    * with the {@linkplain #getInternalParentBeanFactory() internal bean factory} of this
    * context's parent as parent bean factory. Can be overridden in subclasses,
    * for example to customize DefaultListableBeanFactory's settings.
    * @return the bean factory for this context
    * @see org.springframework.beans.factory.support.DefaultListableBeanFactory#setAllowBeanDefinitionOverriding
    * @see org.springframework.beans.factory.support.DefaultListableBeanFactory#setAllowEagerClassLoading
    * @see org.springframework.beans.factory.support.DefaultListableBeanFactory#setAllowCircularReferences
    * @see org.springframework.beans.factory.support.DefaultListableBeanFactory#setAllowRawInjectionDespiteWrapping
    */
   protected DefaultListableBeanFactory createBeanFactory() {
      return new DefaultListableBeanFactory(getInternalParentBeanFactory());
   }

   /**
    * Customize the internal bean factory used by this context.
    * Called for each {@link #refresh()} attempt.
    * <p>The default implementation applies this context's
    * {@linkplain #setAllowBeanDefinitionOverriding "allowBeanDefinitionOverriding"}
    * and {@linkplain #setAllowCircularReferences "allowCircularReferences"} settings,
    * if specified. Can be overridden in subclasses to customize any of
    * {@link DefaultListableBeanFactory}'s settings.
    * @param beanFactory the newly created bean factory for this context
    * @see DefaultListableBeanFactory#setAllowBeanDefinitionOverriding
    * @see DefaultListableBeanFactory#setAllowCircularReferences
    * @see DefaultListableBeanFactory#setAllowRawInjectionDespiteWrapping
    * @see DefaultListableBeanFactory#setAllowEagerClassLoading
    */
   protected void customizeBeanFactory(DefaultListableBeanFactory beanFactory) {
      if (this.allowBeanDefinitionOverriding != null) {
         beanFactory.setAllowBeanDefinitionOverriding(this.allowBeanDefinitionOverriding);
      }
      if (this.allowCircularReferences != null) {
         beanFactory.setAllowCircularReferences(this.allowCircularReferences);
      }
   }

   /**
    * Load bean definitions into the given bean factory, typically through
    * delegating to one or more bean definition readers.
    * @param beanFactory the bean factory to load bean definitions into
    * @throws BeansException if parsing of the bean definitions failed
    * @throws IOException if loading of bean definition files failed
    * @see org.springframework.beans.factory.support.PropertiesBeanDefinitionReader
    * @see org.springframework.beans.factory.xml.XmlBeanDefinitionReader 
    * 這也是該類提供抽象方法最重要的一個,即:怎麼從BeanFactory中加載xml中定義的類到spirng管理的內存中
    */
   protected abstract void loadBeanDefinitions(DefaultListableBeanFactory beanFactory)
         throws BeansException, IOException;

}

二、DefaultListableBeanFactory的初始化debug

如上繼承圖可知道AbstractAutowireCapableBeanFactroy類爲直接抽象父類code

該類主要是spring建立一個實體類具體實現,核心實現之一;orm

 

而後是ConfigurableListableBeanFactoryxml

這個類能夠看出在AbstractAutowireCapableBeanFactroy實現相同的接口後繼承

AutowireCapableBeanFactory,後實現ListableBeanFactory

ListableBeanFactory:主要是能夠按照各類方式枚舉出符合條件的bean集合,如下方法能夠知道

 

ConfigurableBeanFactory:

這個接口定義的是註冊bean的別名,依賴,設置beanscope等配置bean的功能

 

HierarchicalBeanFactory:繼承層次接口:

SingletonBeanRegistry:註冊單利的接口

 

DefaultListableBeanFactory的功能是自動裝載bean,獲取bean,設置bean配置信息等;

 

public DefaultListableBeanFactory() {
   super();
}

/**
 * Create a new DefaultListableBeanFactory with the given parent.
 * @param parentBeanFactory the parent BeanFactory
 */
public DefaultListableBeanFactory(BeanFactory parentBeanFactory) {
   super(parentBeanFactory);
}

 

*/
public AbstractAutowireCapableBeanFactory() {
   super();
   ignoreDependencyInterface(BeanNameAware.class);
   ignoreDependencyInterface(BeanFactoryAware.class);
   ignoreDependencyInterface(BeanClassLoaderAware.class);
}

/**
 * Create a new AbstractAutowireCapableBeanFactory with the given parent.
 * @param parentBeanFactory parent bean factory, or {@code null} if none
 */
public AbstractAutowireCapableBeanFactory(BeanFactory parentBeanFactory) {
   this();
   setParentBeanFactory(parentBeanFactory);
}
public void ignoreDependencyInterface(Class<?> ifc) {
   this.ignoredDependencyInterfaces.add(ifc);
}

 

*/
public AbstractBeanFactory() {
}

初始化配置裝載bean忽略的依賴接口

相關文章
相關標籤/搜索