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忽略的依赖接口

相关文章
相关标签/搜索