深入理解Spring系列之二:BeanDefinition解析

《深入理解Spring系列之一:開篇》中提到在Spring容器啟動的過程中瞭亮,會將Bean解析成Spring內(nèi)部的BeanDefinition結構,本篇將深入分析這個BeanDefinition的內(nèi)部結構固棚。

直接看BeanDefinition源碼统翩,

public interface BeanDefinition extends AttributeAccessor, BeanMetadataElement {
    /**
     * Scope identifier for the standard singleton scope: "singleton".
     * <p>Note that extended bean factories might support further scopes.
     */
    String SCOPE_SINGLETON = ConfigurableBeanFactory.SCOPE_SINGLETON;
    /**
     * Scope identifier for the standard prototype scope: "prototype".
     * <p>Note that extended bean factories might support further scopes.
     */
    String SCOPE_PROTOTYPE = ConfigurableBeanFactory.SCOPE_PROTOTYPE;
    /**
     * Role hint indicating that a {@code BeanDefinition} is a major part
     * of the application. Typically corresponds to a user-defined bean.
     */
    int ROLE_APPLICATION = 0;
    /**
     * Role hint indicating that a {@code BeanDefinition} is a supporting
     * part of some larger configuration, typically an outer
     * {@link org.springframework.beans.factory.parsing.ComponentDefinition}.
     * {@code SUPPORT} beans are considered important enough to be aware
     * of when looking more closely at a particular
     * {@link org.springframework.beans.factory.parsing.ComponentDefinition},
     * but not when looking at the overall configuration of an application.
     */
    int ROLE_SUPPORT = 1;
    /**
     * Role hint indicating that a {@code BeanDefinition} is providing an
     * entirely background role and has no relevance to the end-user. This hint is
     * used when registering beans that are completely part of the internal workings
     * of a {@link org.springframework.beans.factory.parsing.ComponentDefinition}.
     */
    int ROLE_INFRASTRUCTURE = 2;
    /**
     * Return the name of the parent definition of this bean definition, if any.
     */
    String getParentName();
    /**
     * Set the name of the parent definition of this bean definition, if any.
     */
    void setParentName(String parentName);
    /**
     * Return the current bean class name of this bean definition.
     * <p>Note that this does not have to be the actual class name used at runtime, in
     * case of a child definition overriding/inheriting the class name from its parent.
     * Hence, do <i>not</i> consider this to be the definitive bean type at runtime but
     * rather only use it for parsing purposes at the individual bean definition level.
     */
    String getBeanClassName();
    /**
     * Override the bean class name of this bean definition.
     * <p>The class name can be modified during bean factory post-processing,
     * typically replacing the original class name with a parsed variant of it.
     */
    void setBeanClassName(String beanClassName);
    /**
     * Return the factory bean name, if any.
     */
    String getFactoryBeanName();
    /**
     * Specify the factory bean to use, if any.
     */
    void setFactoryBeanName(String factoryBeanName);
    /**
     * Return a factory method, if any.
     */
    String getFactoryMethodName();
    /**
     * Specify a factory method, if any. This method will be invoked with
     * constructor arguments, or with no arguments if none are specified.
     * The method will be invoked on the specified factory bean, if any,
     * or otherwise as a static method on the local bean class.
     * @param factoryMethodName static factory method name,
     * or {@code null} if normal constructor creation should be used
     */
    void setFactoryMethodName(String factoryMethodName);
    /**
     * Return the name of the current target scope for this bean,
     * or {@code null} if not known yet.
     */
    String getScope();
    /**
     * Override the target scope of this bean, specifying a new scope name.
     */
    void setScope(String scope);
    /**
     * Return whether this bean should be lazily initialized, i.e. not
     * eagerly instantiated on startup. Only applicable to a singleton bean.
     */
    boolean isLazyInit();
    /**
     * Set whether this bean should be lazily initialized.
     * <p>If {@code false}, the bean will get instantiated on startup by bean
     * factories that perform eager initialization of singletons.
     */
    void setLazyInit(boolean lazyInit);
    /**
     * Return the bean names that this bean depends on.
     */
    String[] getDependsOn();
    /**
     * Set the names of the beans that this bean depends on being initialized.
     * The bean factory will guarantee that these beans get initialized first.
     */
    void setDependsOn(String... dependsOn);
    /**
     * Return whether this bean is a candidate for getting autowired into some other bean.
     */
    boolean isAutowireCandidate();
    /**
     * Set whether this bean is a candidate for getting autowired into some other bean.
     */
    void setAutowireCandidate(boolean autowireCandidate);
    /**
     * Return whether this bean is a primary autowire candidate.
     * If this value is true for exactly one bean among multiple
     * matching candidates, it will serve as a tie-breaker.
     */
    boolean isPrimary();
    /**
     * Set whether this bean is a primary autowire candidate.
     * <p>If this value is true for exactly one bean among multiple
     * matching candidates, it will serve as a tie-breaker.
     */
    void setPrimary(boolean primary);
    /**
     * Return the constructor argument values for this bean.
     * <p>The returned instance can be modified during bean factory post-processing.
     * @return the ConstructorArgumentValues object (never {@code null})
     */
    ConstructorArgumentValues getConstructorArgumentValues();
    /**
     * Return the property values to be applied to a new instance of the bean.
     * <p>The returned instance can be modified during bean factory post-processing.
     * @return the MutablePropertyValues object (never {@code null})
     */
    MutablePropertyValues getPropertyValues();
    /**
     * Return whether this a <b>Singleton</b>, with a single, shared instance
     * returned on all calls.
     */
    boolean isSingleton();
    /**
     * Return whether this a <b>Prototype</b>, with an independent instance
     * returned for each call.
     */
    boolean isPrototype();
    /**
     * Return whether this bean is "abstract", that is, not meant to be instantiated.
     */
    boolean isAbstract();
    /**
     * Get the role hint for this {@code BeanDefinition}. The role hint
     * provides the frameworks as well as tools with an indication of
     * the role and importance of a particular {@code BeanDefinition}.
     */
    int getRole();
    /**
     * Return a human-readable description of this bean definition.
     */
    String getDescription();
    /**
     * Return a description of the resource that this bean definition
     * came from (for the purpose of showing context in case of errors).
     */
    String getResourceDescription();
    /**
     * Return the originating BeanDefinition, or {@code null} if none.
     * Allows for retrieving the decorated bean definition, if any.
     * <p>Note that this method returns the immediate originator. Iterate through the
     * originator chain to find the original BeanDefinition as defined by the user.
     */
    BeanDefinition getOriginatingBeanDefinition();
}

可以看到上面的很多屬性和方法都很熟悉,例如類名此洲、scope厂汗、屬性、構造函數(shù)參數(shù)列表呜师、依賴的bean娶桦、是否是單例類、是否是懶加載等汁汗,其實就是將Bean的定義信息存儲到這個BeanDefinition相應的屬性中衷畦,后面對Bean的操作就直接對BeanDefinition進行,例如拿到這個BeanDefinition后碰酝,可以根據(jù)里面的類名霎匈、構造函數(shù)、構造函數(shù)參數(shù)送爸,使用反射進行對象創(chuàng)建铛嘱。BeanDefinition是一個接口暖释,是一個抽象的定義,實際使用的是其實現(xiàn)類墨吓,如ChildBeanDefinition球匕、RootBeanDefinition、GenericBeanDefinition等。BeanDefinition繼承了AttributeAccessor硼莽,說明它具有處理屬性的能力猿推;BeanDefinition繼承了BeanMetadataElement,說明它可以持有Bean元數(shù)據(jù)元素照卦,作用是可以持有XML文件的一個bean標簽對應的Object。

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末乡摹,一起剝皮案震驚了整個濱河市役耕,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌聪廉,老刑警劉巖瞬痘,帶你破解...
    沈念sama閱讀 217,542評論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異板熊,居然都是意外死亡框全,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,822評論 3 394
  • 文/潘曉璐 我一進店門干签,熙熙樓的掌柜王于貴愁眉苦臉地迎上來津辩,“玉大人,你說我怎么就攤上這事筒严〉と” “怎么了?”我有些...
    開封第一講書人閱讀 163,912評論 0 354
  • 文/不壞的土叔 我叫張陵鸭蛙,是天一觀的道長摹恨。 經(jīng)常有香客問我,道長娶视,這世上最難降的妖魔是什么晒哄? 我笑而不...
    開封第一講書人閱讀 58,449評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮肪获,結果婚禮上寝凌,老公的妹妹穿的比我還像新娘。我一直安慰自己孝赫,他們只是感情好较木,可當我...
    茶點故事閱讀 67,500評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著青柄,像睡著了一般伐债。 火紅的嫁衣襯著肌膚如雪预侯。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,370評論 1 302
  • 那天峰锁,我揣著相機與錄音萎馅,去河邊找鬼。 笑死虹蒋,一個胖子當著我的面吹牛糜芳,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播魄衅,決...
    沈念sama閱讀 40,193評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼峭竣,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了徐绑?” 一聲冷哼從身側響起邪驮,我...
    開封第一講書人閱讀 39,074評論 0 276
  • 序言:老撾萬榮一對情侶失蹤莫辨,失蹤者是張志新(化名)和其女友劉穎傲茄,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體沮榜,經(jīng)...
    沈念sama閱讀 45,505評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡盘榨,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,722評論 3 335
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了蟆融。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片草巡。...
    茶點故事閱讀 39,841評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖型酥,靈堂內(nèi)的尸體忽然破棺而出山憨,到底是詐尸還是另有隱情,我是刑警寧澤弥喉,帶...
    沈念sama閱讀 35,569評論 5 345
  • 正文 年R本政府宣布郁竟,位于F島的核電站,受9級特大地震影響由境,放射性物質(zhì)發(fā)生泄漏棚亩。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,168評論 3 328
  • 文/蒙蒙 一虏杰、第九天 我趴在偏房一處隱蔽的房頂上張望讥蟆。 院中可真熱鬧,春花似錦纺阔、人聲如沸瘸彤。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,783評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽质况。三九已至低零,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間拯杠,已是汗流浹背掏婶。 一陣腳步聲響...
    開封第一講書人閱讀 32,918評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留潭陪,地道東北人雄妥。 一個月前我還...
    沈念sama閱讀 47,962評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像依溯,于是被迫代替她去往敵國和親老厌。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,781評論 2 354

推薦閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構建分布式系統(tǒng)中一些常見模式的工具(例如配置管理黎炉,服務發(fā)現(xiàn)枝秤,斷路器,智...
    卡卡羅2017閱讀 134,656評論 18 139
  • 文章作者:Tyan博客:noahsnail.com 3.4 Dependencies A typical ente...
    SnailTyan閱讀 4,158評論 2 7
  • Spring Boot 參考指南 介紹 轉載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 46,810評論 6 342
  • 文章作者:Tyan博客:noahsnail.com 3.4 依賴 標準企業(yè)應用不會由一個對象(或Spring用語中...
    SnailTyan閱讀 1,180評論 0 1
  • 年初慷嗜,網(wǎng)絡上到處都是各APP的年終總結報告淀弹。 看著別人在朋友圈里一個接一個地曬著總結,阿柒想著庆械,既然今年沒有寫年終...
    沒頭腦少女陳較瘦閱讀 262評論 0 0