pigeon源碼分析-與spring整合

pigeon Pigeon開發(fā)指南 中指出竹挡,調(diào)用 pigeon 服務用有多種方式:spring 配置趾疚,annotation方式、spring schema定義方式猩谊、api方式千劈。一般使用傳統(tǒng)的 spring 配置方式,具體示例如下:

<bean id="echoService" class="com.dianping.pigeon.remoting.invoker.config.spring.ReferenceBean" init-method="init">
    <!-- 服務全局唯一的標識url牌捷,默認是服務接口類名墙牌,必須設置 -->
    <property name="url" value="http://service.dianping.com/demoService/echoService_1.0.0" />
    <!-- 接口名稱,必須設置 -->
    <property name="interfaceName" value="com.dianping.pigeon.demo.EchoService" />
    <!-- 超時時間暗甥,毫秒憔古,默認5000,建議自己設置 -->
    <property name="timeout" value="2000" />
    <!-- 序列化淋袖,hessian/fst/protostuff鸿市,默認hessian,可不設置-->
    <property name="serialize" value="hessian" />
    <!-- 調(diào)用方式即碗,sync/future/callback/oneway焰情,默認sync,可不設置 -->
    <property name="callType" value="sync" />
    <!-- 失敗策略剥懒,快速失敗failfast/失敗轉(zhuǎn)移failover/失敗忽略failsafe/并發(fā)取最快返回forking内舟,默認failfast,可不設置 -->
    <property name="cluster" value="failfast" />
    <!-- 是否超時重試初橘,默認false验游,可不設置 -->
    <property name="timeoutRetry" value="false" />
    <!-- 重試次數(shù),默認1保檐,可不設置 -->
    <property name="retries" value="1" />
</bean>

然后耕蝉,使用時,在相應的 field 上使用 @Autowire 注解指明即可夜只,如下:

@Controller
public class Demo {
    @Autowired
    private EchoService echoService;
}

那么問題來了垒在,被注入的 Field 的類型是 EchoService,但是配置的 bean 是的類型是 ReferenceBean扔亥,怎么匹配上的场躯?再延伸一下谈为,所有的 service 在調(diào)用聲明的時候都是 ReferenceBean 類型,但是被注入的 field 類型不定踢关,又是怎么分別匹配的伞鲫?

ReferenceBean 詳解

看下 ReferenceBean 的定義就知道了,實現(xiàn)了 FactoryBean 接口签舞,看下這個接口的定義是什么:

 * Interface to be implemented by objects used within a {@link BeanFactory}
 * which are themselves factories. If a bean implements this interface,
 * it is used as a factory for an object to expose, not directly as a bean
 * instance that will be exposed itself.
 *
 * <p><b>NB: A bean that implements this interface cannot be used as a
 * normal bean.</b> A FactoryBean is defined in a bean style, but the
 * object exposed for bean references ({@link #getObject()} is always
 * the object that it creates.
 *
 * <p>FactoryBeans can support singletons and prototypes, and can
 * either create objects lazily on demand or eagerly on startup.
 * The {@link SmartFactoryBean} interface allows for exposing
 * more fine-grained behavioral metadata.
 *
 * <p>This interface is heavily used within the framework itself, for
 * example for the AOP {@link org.springframework.aop.framework.ProxyFactoryBean}
 * or the {@link org.springframework.jndi.JndiObjectFactoryBean}.
 * It can be used for application components as well; however,
 * this is not common outside of infrastructure code.

總的來說榔昔,這是提供 bean 的一種方式,真正提供的產(chǎn)物是 #getObejct() 方法生成的 bean瘪菌,文檔也提到侦另,框架內(nèi)部大量使用這個接口提供 bean

看看接口內(nèi)容:

public interface FactoryBean<T> {
    T getObject() throws Exception;
    Class<?> getObjectType();
    boolean isSingleton();
}

pigeon 的 ReferenceBean 實現(xiàn)中叹阔, isSingleton() 方法總是返回 true餐禁,就是說生成的都是單例

getObject() 方法返回真正的 bean镀虐,那么它是在哪里生成的呢?答案就是 spring 配置中的 init() 方法默穴。

init 方法

來看方法的實現(xiàn):

    public void init() throws Exception {
        if (StringUtils.isBlank(interfaceName)) {
            throw new IllegalArgumentException("invalid interface:" + interfaceName);
        }
        this.objType = ClassUtils.loadClass(this.classLoader, this.interfaceName.trim());
        InvokerConfig<?> invokerConfig = new InvokerConfig(this.objType, this.url, this.timeout, this.callType,
                this.serialize, this.callback, this.suffix, this.writeBufferLimit, this.loadBalance, this.cluster,
                this.retries, this.timeoutRetry, this.vip, this.version, this.protocol);
        invokerConfig.setClassLoader(classLoader);
        // ...

        this.obj = ServiceFactory.getService(invokerConfig);
        configLoadBalance(invokerConfig);
    }

設置了 InvokerConfig怔檩,之后生成代理,this.Obj 就是 FactoryBean 返回的對象蓄诽。

具體在使用的時候薛训,Autowire 注解的 field 在注入時會觸發(fā)從 BeanFactory 獲取相應的 bean,此時根據(jù) field 的類型查找 candidate bean仑氛,此時遍歷 BeanDefinition乙埃,如果發(fā)現(xiàn)是 FactoryBean,則會做一些標記锯岖,然后 getBean() 時就從 FactoryBean 獲取介袜。具體邏輯在:org.springframework.beans.factory.support.DefaultListableBeanFactory#findAutowireCandidates ,這里就不詳細展開了出吹。

總的來說遇伞,就是 FactoryBean 和 Autowire 機制的合理使用。

?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末捶牢,一起剝皮案震驚了整個濱河市鸠珠,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌秋麸,老刑警劉巖渐排,帶你破解...
    沈念sama閱讀 217,277評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異竹勉,居然都是意外死亡飞盆,警方通過查閱死者的電腦和手機娄琉,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,689評論 3 393
  • 文/潘曉璐 我一進店門次乓,熙熙樓的掌柜王于貴愁眉苦臉地迎上來吓歇,“玉大人,你說我怎么就攤上這事票腰〕强矗” “怎么了?”我有些...
    開封第一講書人閱讀 163,624評論 0 353
  • 文/不壞的土叔 我叫張陵杏慰,是天一觀的道長测柠。 經(jīng)常有香客問我,道長缘滥,這世上最難降的妖魔是什么轰胁? 我笑而不...
    開封第一講書人閱讀 58,356評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮朝扼,結(jié)果婚禮上赃阀,老公的妹妹穿的比我還像新娘。我一直安慰自己擎颖,他們只是感情好榛斯,可當我...
    茶點故事閱讀 67,402評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著搂捧,像睡著了一般驮俗。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上允跑,一...
    開封第一講書人閱讀 51,292評論 1 301
  • 那天王凑,我揣著相機與錄音,去河邊找鬼聋丝。 笑死荤崇,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的潮针。 我是一名探鬼主播术荤,決...
    沈念sama閱讀 40,135評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼每篷!你這毒婦竟也來了瓣戚?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,992評論 0 275
  • 序言:老撾萬榮一對情侶失蹤焦读,失蹤者是張志新(化名)和其女友劉穎子库,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體矗晃,經(jīng)...
    沈念sama閱讀 45,429評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡仑嗅,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,636評論 3 334
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片仓技。...
    茶點故事閱讀 39,785評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡鸵贬,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出脖捻,到底是詐尸還是另有隱情阔逼,我是刑警寧澤,帶...
    沈念sama閱讀 35,492評論 5 345
  • 正文 年R本政府宣布地沮,位于F島的核電站嗜浮,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏摩疑。R本人自食惡果不足惜危融,卻給世界環(huán)境...
    茶點故事閱讀 41,092評論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望雷袋。 院中可真熱鬧专挪,春花似錦、人聲如沸片排。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,723評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽率寡。三九已至迫卢,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間冶共,已是汗流浹背乾蛤。 一陣腳步聲響...
    開封第一講書人閱讀 32,858評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留捅僵,地道東北人家卖。 一個月前我還...
    沈念sama閱讀 47,891評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像庙楚,于是被迫代替她去往敵國和親上荡。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,713評論 2 354

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