Spring之BeanPostProcessor接口

BeanPostProcessor接口是Spring IOC容器給我們提供的一個擴展接口,接口定義如下:

/*
  
package org.springframework.beans.factory.config;

import org.springframework.beans.BeansException;
import org.springframework.lang.Nullable;

/**
 * Factory hook that allows for custom modification of new bean instances —
 * for example, checking for marker interfaces or wrapping beans with proxies.
 *
 * <p>Typically, post-processors that populate beans via marker interfaces
 * or the like will implement {@link #postProcessBeforeInitialization},
 * while post-processors that wrap beans with proxies will normally
 * implement {@link #postProcessAfterInitialization}.
 *
 * <h3>Registration</h3>
 * <p>An {@code ApplicationContext} can autodetect {@code BeanPostProcessor} beans
 * in its bean definitions and apply those post-processors to any beans subsequently
 * created. A plain {@code BeanFactory} allows for programmatic registration of
 * post-processors, applying them to all beans created through the bean factory.
 *
 * <h3>Ordering</h3>
 * <p>{@code BeanPostProcessor} beans that are autodetected in an
 * {@code ApplicationContext} will be ordered according to
 * {@link org.springframework.core.PriorityOrdered} and
 * {@link org.springframework.core.Ordered} semantics. In contrast,
 * {@code BeanPostProcessor} beans that are registered programmatically with a
 * {@code BeanFactory} will be applied in the order of registration; any ordering
 * semantics expressed through implementing the
 * {@code PriorityOrdered} or {@code Ordered} interface will be ignored for
 * programmatically registered post-processors. Furthermore, the
 * {@link org.springframework.core.annotation.Order @Order} annotation is not
 * taken into account for {@code BeanPostProcessor} beans.
 *
 * @author Juergen Hoeller
 * @author Sam Brannen
 * @since 10.10.2003
 * @see InstantiationAwareBeanPostProcessor
 * @see DestructionAwareBeanPostProcessor
 * @see ConfigurableBeanFactory#addBeanPostProcessor
 * @see BeanFactoryPostProcessor
 */
public interface BeanPostProcessor {

    /**
     * Apply this {@code BeanPostProcessor} to the given new bean instance <i>before</i> any bean
     * initialization callbacks (like InitializingBean's {@code afterPropertiesSet}
     * or a custom init-method). The bean will already be populated with property values.
     * The returned bean instance may be a wrapper around the original.
     * <p>The default implementation returns the given {@code bean} as-is.
     * @param bean the new bean instance
     * @param beanName the name of the bean
     * @return the bean instance to use, either the original or a wrapped one;
     * if {@code null}, no subsequent BeanPostProcessors will be invoked
     * @throws org.springframework.beans.BeansException in case of errors
     * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet
     */
    @Nullable
    default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

    /**
     * Apply this {@code BeanPostProcessor} to the given new bean instance <i>after</i> any bean
     * initialization callbacks (like InitializingBean's {@code afterPropertiesSet}
     * or a custom init-method). The bean will already be populated with property values.
     * The returned bean instance may be a wrapper around the original.
     * <p>In case of a FactoryBean, this callback will be invoked for both the FactoryBean
     * instance and the objects created by the FactoryBean (as of Spring 2.0). The
     * post-processor can decide whether to apply to either the FactoryBean or created
     * objects or both through corresponding {@code bean instanceof FactoryBean} checks.
     * <p>This callback will also be invoked after a short-circuiting triggered by a
     * {@link InstantiationAwareBeanPostProcessor#postProcessBeforeInstantiation} method,
     * in contrast to all other {@code BeanPostProcessor} callbacks.
     * <p>The default implementation returns the given {@code bean} as-is.
     * @param bean the new bean instance
     * @param beanName the name of the bean
     * @return the bean instance to use, either the original or a wrapped one;
     * if {@code null}, no subsequent BeanPostProcessors will be invoked
     * @throws org.springframework.beans.BeansException in case of errors
     * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet
     * @see org.springframework.beans.factory.FactoryBean
     */
    @Nullable
    default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

}

在Spring啟動過程中梭纹,當BeanPostProcessor的接口實現(xiàn)類(如AbstractAutoProxyCreator、DefaultAdvisorAutoProxyCreator等)注冊到Spring IOC容器后進行如下操作:
a.對于IOC容器所創(chuàng)建的每個bean實例在初始化方法(如afterPropertiesSet和init方法)調(diào)用前臭增,將會調(diào)用BeanPostProcessor中的

Object postProcessBeforeInitialization(Object bean, String beanName) ;

方法紧显;
b.在bean實例初始化方法(如afterPropertiesSet和init方法)調(diào)用完成后,則會調(diào)用BeanPostProcessor中的

Object postProcessAfterInitialization(Object bean, String beanName);

方法肥印。

具體過程如下:

1.Spring IOC容器實例化Bean;
2.調(diào)用BeanPostProcessor接口實現(xiàn)類中的postProcessBeforeInitialization方法;
3.調(diào)用bean實例的初始化方法(afterPropertiesSet()和init()方法);
4.調(diào)用BeanPostProcessor的postProcessAfterInitialization方法。

在實際項目開發(fā)中绝葡,Spring IOC容器中會配置多個BeanPostProcessor接口實現(xiàn)類深碱,在默認情況下Spring IOC容器會根據(jù)BeanPostProcessor接口實現(xiàn)類的定義順序來依次調(diào)用。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末藏畅,一起剝皮案震驚了整個濱河市敷硅,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌愉阎,老刑警劉巖绞蹦,帶你破解...
    沈念sama閱讀 212,185評論 6 493
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異榜旦,居然都是意外死亡幽七,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,445評論 3 385
  • 文/潘曉璐 我一進店門溅呢,熙熙樓的掌柜王于貴愁眉苦臉地迎上來澡屡,“玉大人,你說我怎么就攤上這事咐旧∈火模” “怎么了?”我有些...
    開封第一講書人閱讀 157,684評論 0 348
  • 文/不壞的土叔 我叫張陵铣墨,是天一觀的道長梁厉。 經(jīng)常有香客問我,道長踏兜,這世上最難降的妖魔是什么词顾? 我笑而不...
    開封第一講書人閱讀 56,564評論 1 284
  • 正文 為了忘掉前任,我火速辦了婚禮碱妆,結(jié)果婚禮上肉盹,老公的妹妹穿的比我還像新娘。我一直安慰自己疹尾,他們只是感情好上忍,可當我...
    茶點故事閱讀 65,681評論 6 386
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著纳本,像睡著了一般窍蓝。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上繁成,一...
    開封第一講書人閱讀 49,874評論 1 290
  • 那天吓笙,我揣著相機與錄音,去河邊找鬼巾腕。 笑死面睛,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的尊搬。 我是一名探鬼主播叁鉴,決...
    沈念sama閱讀 39,025評論 3 408
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼佛寿!你這毒婦竟也來了幌墓?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,761評論 0 268
  • 序言:老撾萬榮一對情侶失蹤冀泻,失蹤者是張志新(化名)和其女友劉穎常侣,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體腔长,經(jīng)...
    沈念sama閱讀 44,217評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡袭祟,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,545評論 2 327
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了捞附。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片巾乳。...
    茶點故事閱讀 38,694評論 1 341
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖鸟召,靈堂內(nèi)的尸體忽然破棺而出胆绊,到底是詐尸還是另有隱情,我是刑警寧澤欧募,帶...
    沈念sama閱讀 34,351評論 4 332
  • 正文 年R本政府宣布压状,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏种冬。R本人自食惡果不足惜镣丑,卻給世界環(huán)境...
    茶點故事閱讀 39,988評論 3 315
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望娱两。 院中可真熱鬧莺匠,春花似錦、人聲如沸十兢。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,778評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽旱物。三九已至遥缕,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間宵呛,已是汗流浹背单匣。 一陣腳步聲響...
    開封第一講書人閱讀 32,007評論 1 266
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留烤蜕,地道東北人封孙。 一個月前我還...
    沈念sama閱讀 46,427評論 2 360
  • 正文 我出身青樓,卻偏偏與公主長得像讽营,于是被迫代替她去往敵國和親虎忌。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 43,580評論 2 349