Spring的beanFactory初始化过程分析

这两天对Dubbo代码里面一个使用 FactoryBean的地方有很多疑惑, 才发现自己对我们最熟悉的Spring框架好多地方,都没搞清楚为什么要这样用。
废话少说, Spring作为一个IOC工厂, 自然是为了生产Bean而生,那么就去看下Spring是如何管理,并生产Bean的。
我直接用 AnnotationConfigApplicationContext 来测试,

public static void main(String[] args) throws Exception {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class);
    Dog dog = (Dog) context.getBean("dog");
    dog.test();
    context.start();
    System.in.read();
}

@Configuration
@ComponentScan(value = "org.apache.dubbo.demo.provider")
public class TestConfiguration {
    
}

在new 一个 AnnotationConfigApplicationContext 的时候,先注册我们的Configuration, 然后refresh() 刷新容器启动。

/**
	 * Create a new AnnotationConfigApplicationContext, deriving bean definitions
	 * from the given annotated classes and automatically refreshing the context.
	 * @param annotatedClasses one or more annotated classes,
	 * e.g. {@link Configuration @Configuration} classes
	 */
	public AnnotationConfigApplicationContext(Class<?>... annotatedClasses) {
		this();
		register(annotatedClasses);
		refresh();
	}

refresh方法是继承于AbstractApplicationContext抽象类,是在接口ConfigurableApplicationContext中定义的。其在抽象类中源码如下:

@Override
public void refresh() throws BeansException, IllegalStateException {
    synchronized (this.startupShutdownMonitor) {
        // Prepare this context for refreshing.
        prepareRefresh();

        // Tell the subclass to refresh the internal bean factory.
        ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

        // Prepare the bean factory for use in this context.
        prepareBeanFactory(beanFactory);

        try {
            // Allows post-processing of the bean factory in context subclasses.
            postProcessBeanFactory(beanFactory);

            // Invoke factory processors registered as beans in the context.
            invokeBeanFactoryPostProcessors(beanFactory);

            // Register bean processors that intercept bean creation.
            registerBeanPostProcessors(beanFactory);

            // Initialize message source for this context.
            initMessageSource();

            // Initialize event multicaster for this context.
            initApplicationEventMulticaster();

            // Initialize other special beans in specific context subclasses.
            onRefresh();

            // Check for listener beans and register them.
            registerListeners();

            // Instantiate all remaining (non-lazy-init) singletons.
            finishBeanFactoryInitialization(beanFactory);

            // Last step: publish corresponding event.
            finishRefresh();
        }

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

        finally {
            // Reset common introspection caches in Spring's core, since we
            // might not ever need metadata for singleton beans anymore...
            resetCommonCaches();
        }
    }
}

refresh方法大致描述了Spring IOC容器的初始化过程,第一步prepareRefresh主要是做一些准备工作,如准备应用环境、设置启动时间、设置属性源初始化标志
等。

/**
* Prepare this context for refreshing, setting its startup date and
* active flag as well as performing any initialization of property sources.
*/
protected void prepareRefresh() {
    this.startupDate = System.currentTimeMillis();
    this.closed.set(false);
    this.active.set(true);

    if (logger.isInfoEnabled()) {
        logger.info("Refreshing " + this);
    }

    // Initialize any placeholder property sources in the context environment
    initPropertySources();

    // Validate that all properties marked as required are resolvable
    // see ConfigurablePropertyResolver#setRequiredProperties
    getEnvironment().validateRequiredProperties();

    // Allow for the collection of early ApplicationEvents,
    // to be published once the multicaster is available...
    this.earlyApplicationEvents = new LinkedHashSet<ApplicationEvent>();
}

第二步ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory(),这一步是获取更新后的子类Bean工厂。代码如下:

/**
    * Tell the subclass to refresh the internal bean factory.
    * @return the fresh BeanFactory instance
    * @see #refreshBeanFactory()
    * @see #getBeanFactory()
    */
protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {
    refreshBeanFactory();
    ConfigurableListableBeanFactory beanFactory = getBeanFactory();
    if (logger.isDebugEnabled()) {
        logger.debug("Bean factory for " + getDisplayName() + ": " + beanFactory);
    }
    return beanFactory;
}

两个方法refreshBeanFactory()getBeanFactory(),这两个方法均为抽象方法,在子类 refreshBeanFactory 实现,如下所示:

	/**
	 * 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);
    }
}

先判断是否存在BeanFactory,若存在则直接销毁原BeanFactory,先销毁工厂中的Beans,再关闭工bean厂。之后创建新的Bean工厂,其方法为createBeanFactory,如下所示:

protected DefaultListableBeanFactory createBeanFactory() {
    return new DefaultListableBeanFactory(getInternalParentBeanFactory());
}

创建的Bean工厂是默认的bean工厂DefaultListableBeanFactory类型。创建完新的bean工厂后便会根据上下文进行初始化(customizeBeanFactory) 定制化,加载bean定义(loadBeanDefinitions)。其中,loadBeanDefinitions 为抽象方法,

你可能感兴趣的:(Spring)