Spring BeanFactory 初始化 和 Bean 生命週期

(version:spring-context-4.3.15.RELEASE)html

AbstractApplicationContext#refresh()spring

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

    // Tell the subclass to refresh the internal bean factory.
    // 1. 建立一個 BeanFactory 的實例(DefaultListableBeanFactory)
    // 2. 加載bean的定義 BeanDefinition,並註冊到 beanFactory 中
    // loadBeanDefinitions(beanFactory) --> registerBeanDefinitions()
    ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

    // Prepare the bean factory for use in this context.
    // 給 beanFactory 作上些前期配置:
    // 1. 添加 BeanPostProcessor
    // 2. 註冊一些能夠解析的依賴和 bean,如:BeanFactory, ApplicationContext
    prepareBeanFactory(beanFactory);

    // Allows post-processing of the bean factory in context subclasses.
    // 容許子類對 beanFactory 對象作一些後置處理
    postProcessBeanFactory(beanFactory);

    // Invoke factory processors registered as beans in the context.
    // 調用 beanFactoryPostProcessor,容許用戶對 beanFactory 作修改(擴展點),好比添加、修改或刪除 beanFactory 中註冊的 BeanDefinition 等
    // 按順序調用:PriorityOrdered 類型的 --> Ordered 類型的 --> the rest
    invokeBeanFactoryPostProcessors(beanFactory);

    // Register bean processors that intercept bean creation.
    // 註冊 beanPostProcessor 到 beanFactory 中
    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.
    // 完成 beanFactory 的初始化:解析 bean 的依賴,完成 bean 實例的初始化
    finishBeanFactoryInitialization(beanFactory);

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

 

 

beanFactory 實例化後,會向 beanFactory 註冊 bean 的定義 BeanDefinition。
最後 beanFactory 會進行初始化,執行 finishBeanFactoryInitialization(beanFactory) 時就開始了 bean 的生命週期app

bean 的生命週期:
1. 實例化 Instantiation
2. 屬性賦值 Populate
3. 初始化 Initialization
4. 銷燬 Destructionpost

實例化 -> 屬性賦值 -> 初始化 -> 銷燬

 

// 忽略了無關代碼
protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final @Nullable Object[] args) throws BeanCreationException {

    // Instantiate the bean.
    BeanWrapper instanceWrapper = null;
    if (instanceWrapper == null) {
        // 實例化階段!
        instanceWrapper = createBeanInstance(beanName, mbd, args);
    }

    // Initialize the bean instance.
    Object exposedObject = bean;
    try {
        // 屬性賦值階段!填充 bean 屬性:這一步主要是對bean的依賴屬性進行注入(@Autowired)
        populateBean(beanName, mbd, instanceWrapper);
        // 初始化階段!
        exposedObject = initializeBean(beanName, exposedObject, mbd);
    }

}

 

BeanPostProcessor 和 BeanPostProcessor 執行順序相同:this

PriorityOrdered 類型的 --> Ordered 類型的 --> the rest

 

 

 

參考:
https://www.jianshu.com/p/1dec08d290c1
http://www.javashuo.com/article/p-dfqsnysx-bz.htmlspa

相關文章
相關標籤/搜索