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 機制的合理使用。