<dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo-demo-api</artifactId> <version>${project.parent.version}</version> </dependency>
基於dubbo.xsd中描述的擴展spring schema類配置xmljava
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- 消費方應用名,用於計算依賴關係,不是匹配條件,不要與提供方同樣 --> <dubbo:application name="demo-consumer"/> <!-- 使用multicast廣播註冊中心暴露發現服務地址 --> <!--<dubbo:registry address="multicast://224.5.6.7:1234"/>--> <dubbo:registry address="zookeeper://127.0.0.1:2181"/> <!-- 生成遠程服務代理,能夠和本地bean同樣使用demoService --> <dubbo:reference id="demoService" check="false" interface="com.alibaba.dubbo.demo.DemoService"/> <bean class="com.alibaba.dubbo.demo.consumer.DemoAction" init-method="start"> <property name="demoService" ref="demoService"/> </bean> </beans>
經過set的方式來注入DemoService屬性【spring Ioc的一種注入方式】spring
public class DemoAction { private DemoService demoService; public void setDemoService(DemoService demoService) { this.demoService = demoService; } ... }
這一些列操做完了,在conusmer端,咱們並無看到DemoService的任何實現類。那麼問題來了,segmentfault
從https://segmentfault.com/a/11...,咱們能夠類比得知,dubbo:reference會映射成一個ReferenceBeanapi
public class ReferenceBean<T> extends ReferenceConfig<T> implements FactoryBean, ApplicationContextAware, InitializingBean, DisposableBean { private static final long serialVersionUID = 213195494150089726L; private transient ApplicationContext applicationContext; public ReferenceBean() { super(); } ... public Object getObject() throws Exception { return get(); } public Class<?> getObjectType() { return getInterfaceClass(); } @Parameter(excluded = true) public boolean isSingleton() { return true; } ... }
發現ReferenceBean實現了FactoryBean接口,FactoryBean是一個Java Bean,可是它是一個能生產對象的工廠Bean。經過FactoryBean#getObject咱們就能夠拿到該對象的實例。緩存
public interface FactoryBean<T> { //返回由FactoryBean建立的bean實例,若是isSingleton()返回true,則該實例會放到Spring容器中單實例緩存池中。 T getObject() throws Exception; //返回FactoryBean建立的bean類型。 Class<?> getObjectType(); //返回由FactoryBean建立的bean實例的做用域是singleton仍是prototype。 boolean isSingleton(); }
那麼FactoryBean#getObject到底都發生了啥,下面咱們來粗略的看一下。app