Class ReferenceBean<T>

java.lang.Object
org.apache.dubbo.config.spring.ReferenceBean<T>
All Implemented Interfaces:
org.springframework.beans.factory.Aware, org.springframework.beans.factory.BeanClassLoaderAware, org.springframework.beans.factory.BeanNameAware, org.springframework.beans.factory.DisposableBean, org.springframework.beans.factory.FactoryBean<T>, org.springframework.beans.factory.InitializingBean, org.springframework.context.ApplicationContextAware

public class ReferenceBean<T> extends Object implements org.springframework.beans.factory.FactoryBean<T>, org.springframework.context.ApplicationContextAware, org.springframework.beans.factory.BeanClassLoaderAware, org.springframework.beans.factory.BeanNameAware, org.springframework.beans.factory.InitializingBean, org.springframework.beans.factory.DisposableBean

Spring FactoryBean for ReferenceConfig.

Step 1: Register ReferenceBean in Java-config class:
 @Configuration
 public class ReferenceConfiguration {
     @Bean
     @DubboReference(group = "demo")
     public ReferenceBean<HelloService> helloService() {
         return new ReferenceBean();
     }

     // As GenericService
     @Bean
     @DubboReference(group = "demo", interfaceClass = HelloService.class)
     public ReferenceBean<GenericService> genericHelloService() {
         return new ReferenceBean();
     }
 }
 

Or register ReferenceBean in xml:

 <dubbo:reference id="helloService" interface="org.apache.dubbo.config.spring.api.HelloService"/>
 <!-- As GenericService -->
 <dubbo:reference id="genericHelloService" interface="org.apache.dubbo.config.spring.api.HelloService" generic="true"/>
 

Step 2: Inject ReferenceBean by @Autowired

 public class FooController {
     @Autowired
     private HelloService helloService;

     @Autowired
     private GenericService genericHelloService;
 }
 
See Also:
  • Constructor Details

    • ReferenceBean

      public ReferenceBean()
    • ReferenceBean

      public ReferenceBean(Map<String,Object> referenceProps)
  • Method Details

    • setApplicationContext

      public void setApplicationContext(org.springframework.context.ApplicationContext applicationContext)
      Specified by:
      setApplicationContext in interface org.springframework.context.ApplicationContextAware
    • setBeanClassLoader

      public void setBeanClassLoader(ClassLoader classLoader)
      Specified by:
      setBeanClassLoader in interface org.springframework.beans.factory.BeanClassLoaderAware
    • setBeanName

      public void setBeanName(String name)
      Specified by:
      setBeanName in interface org.springframework.beans.factory.BeanNameAware
    • getObject

      public T getObject()
      Create bean instance.

      Why we need a lazy proxy?

      When Spring searches beans by type, if Spring cannot determine the type of a factory bean, it may try to initialize it. The ReferenceBean is also a FactoryBean.
      (This has already been resolved by decorating the BeanDefinition: DubboBeanDefinitionParser.configReferenceBean(org.w3c.dom.Element, org.springframework.beans.factory.xml.ParserContext, org.springframework.beans.factory.support.RootBeanDefinition, org.springframework.beans.factory.config.BeanDefinition))

      In addition, if some ReferenceBeans are dependent on beans that are initialized very early, and dubbo config beans are not ready yet, there will be many unexpected problems if initializing the dubbo reference immediately.

      When it is initialized, only a lazy proxy object will be created, and dubbo reference-related resources will not be initialized.
      In this way, the influence of Spring is eliminated, and the dubbo configuration initialization is controllable.

      Specified by:
      getObject in interface org.springframework.beans.factory.FactoryBean<T>
      See Also:
    • getObjectType

      public Class<?> getObjectType()
      Specified by:
      getObjectType in interface org.springframework.beans.factory.FactoryBean<T>
    • isSingleton

      @Parameter(excluded=true) public boolean isSingleton()
      Specified by:
      isSingleton in interface org.springframework.beans.factory.FactoryBean<T>
    • afterPropertiesSet

      public void afterPropertiesSet() throws Exception
      Specified by:
      afterPropertiesSet in interface org.springframework.beans.factory.InitializingBean
      Throws:
      Exception
    • destroy

      public void destroy()
      Specified by:
      destroy in interface org.springframework.beans.factory.DisposableBean
    • getId

      public String getId()
    • setId

      public void setId(String id)
    • getInterfaceClass

      public Class<?> getInterfaceClass()
      The interface of this ReferenceBean, for injection purpose
      Returns:
    • getServiceInterface

      public String getServiceInterface()
      The interface of remote service
    • getGroup

      public String getGroup()
      The group of the service
    • getVersion

      public String getVersion()
      The version of the service
    • getKey

      public String getKey()
    • getReferenceProps

      public Map<String,Object> getReferenceProps()
    • getPropertyValues

      public org.springframework.beans.MutablePropertyValues getPropertyValues()
    • getReferenceConfig

      public ReferenceConfig getReferenceConfig()
    • setKeyAndReferenceConfig

      public void setKeyAndReferenceConfig(String key, ReferenceConfig referenceConfig)
    • setInterfaceClass

      public void setInterfaceClass(Class<?> interfaceClass)
    • setInterfaceName

      public void setInterfaceName(String interfaceName)
    • setReferencePropsJson

      public void setReferencePropsJson(String referencePropsJson)
      It is only used in native scenarios to get referenceProps because attribute is not passed by BeanDefinition by default.
      Parameters:
      referencePropsJson -