日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring源码系列:BeanFactory的创建

發布時間:2025/3/17 javascript 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring源码系列:BeanFactory的创建 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

Spring的Ioc容器其實就是一個bean的關系網,依賴于core,bean,context三個組件來構建的。在spring中最核心的就是對于bean的管理。而bean又依托于我們的容器。本文將從頂層分析一下spring中beanFactory的具體創建過程,為后續的bean的生命周期提供一個基礎。 BeanFactory的繼承體系

從上圖可以看到,BeanFactory有三個子類:ListableBeanFactory

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?HierarchicalBeanFactory

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?AutowireCapableBeanFactory

(上述三個類的子類體系小伙伴們可以自己對著源碼看下,實在太多)

看下上圖中最底層的DefaultListableBeanFactory類的定義:

public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFactory implements ConfigurableListableBeanFactory, BeanDefinitionRegistry, Serializable?

這個其實就是BeanFactory的默認實現類,它直接或者間接的實現了所有的接口。其實在看spring源碼的時候都會遇到類似的設計模式,對于某一個具體的功能,通常都會定義很多層的接口,層層包裝,層層委托。這種做法的好處就是,對于不同的場合都會有特定的接口;這樣一來就可以在spring內部對對象的傳遞和轉化操作都會有一些訪問限制。 例如ListableBeanFactory接口表示這些Bean是可列表的,而HierarchicalBeanFactory表示的是這些Bean是有繼承關系的,也就是每個Bean有可能有父Bean。AutowireCapableBeanFactory接口定義Bean的自動裝配規則。這四個接口共同定義了Bean的集合、Bean之間的關系、以及Bean行為。

BeanFactory的創建

在之前的文章中說過了容器的刷新過程。BeanFactory的創建也在wac.refresh()方法中。具體看下到底是通過哪些子類來完成的:

// 通知子類刷新內部的bean工廠。 ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory(); ?

1.AbstractApplicationContext中的obtainFreshBeanFactory

下面是obtainFreshBeanFactory的方法邏輯:

protected ConfigurableListableBeanFactory obtainFreshBeanFactory() { //這個是具體創建的方法,由子類實現 refreshBeanFactory(); //獲取BeanFactory實例對象(ConfigurableListableBeanFactory類型的) ConfigurableListableBeanFactory beanFactory = getBeanFactory(); if (logger.isDebugEnabled()) { logger.debug("Bean factory for " + getDisplayName() + ": " + beanFactory); } return beanFactory; }?

refreshBeanFactory并未有具體的實現邏輯,這個方法主要是通過委托給子類的refreshBeanFactory方法來實現,在AbstractApplicationContext中refreshBeanFactory是一個抽象模板方法:

protected abstract void refreshBeanFactory() throws BeansException, IllegalStateException;?

2.refreshBeanFactory方法(AbstractRefreshableApplicationContext類中):

下面只注釋與beanFactory創建相關的代碼

protected final void refreshBeanFactory() throws BeansException { //是否已經有BeanFactory了 if (hasBeanFactory()) { //銷毀原有的Bean destroyBeans(); //關閉工廠 closeBeanFactory(); } try { //創建一個新的beanFactory 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); } }??

這個方法是實現執行這個上下文的底層bean工廠的實際刷新,如果有的話之前有BeanFactory存在,則關閉以前的bean工廠。并為上下文生命周期的下一個階段初始化一個新鮮的bean工廠。

3.createBeanFactory(AbstractRefreshableApplicationContext類中)

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

這個方法就是為當前上下文創建一個內部的bean工廠。每次調用refresh()方法是都會創建嘗試創建。默認實現是創建一個DefaultListableBeanFactory。并通過getInternalParentBeanFactory()獲取內部bean工廠來作為父級bean工廠??梢栽谧宇愔兄貙?#xff0c;例如自定義DefaultListableBeanFactory的設置。

getInternalParentBeanFactory(AbstractApplicationContext類中)

protected BeanFactory getInternalParentBeanFactory() { return (getParent() instanceof ConfigurableApplicationContext) ? ((ConfigurableApplicationContext) getParent()).getBeanFactory() : getParent(); }??

4.DefaultListableBeanFactory的構造函數

/**

?* 通過給定的父類創建一個新的DefaultListableBeanFactory容器

?* @param parentBeanFactory the parent BeanFactory

?*/

public DefaultListableBeanFactory(BeanFactory parentBeanFactory) { super(parentBeanFactory);

}?

super(parentBeanFactory)調用的是AbstractAutowireCapableBeanFactory的構造函數

/**

?* 通過給定的父類構建新的AbstractAutowireCapableBeanFactory

?* @param parentBeanFactory parent bean factory, or {@code null} if none

?*/

public AbstractAutowireCapableBeanFactory(BeanFactory parentBeanFactory) {

? ? this();

? //設置父工廠

? setParentBeanFactory(parentBeanFactory);

}?

this(),還是AbstractAutowireCapableBeanFactory的構造函數:

/**

?* 構建一個新的AbstractAutowireCapableBeanFactory.

?*/

public AbstractAutowireCapableBeanFactory() {

? ? ?super();

? ? ?ignoreDependencyInterface(BeanNameAware.class);

? ? ?ignoreDependencyInterface(BeanFactoryAware.class);

? ? ?ignoreDependencyInterface(BeanClassLoaderAware.class);

? ? ?}?

super() ; AbstractBeanFactory的構造函數

/**

?* 構建一個新的AbstractBeanFactory.

?*/

public AbstractBeanFactory() {

? }

轉載于:https://my.oschina.net/u/3959468/blog/2052399

新人創作打卡挑戰賽發博客就能抽獎!定制產品紅包拿不停!

總結

以上是生活随笔為你收集整理的Spring源码系列:BeanFactory的创建的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。