You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@dubbo.apache.org by GitBox <gi...@apache.org> on 2021/07/06 07:31:10 UTC

[GitHub] [dubbo] kylixs edited a comment on issue #8221: dubbo consumer端对 方法自定义引入引发启动报错

kylixs edited a comment on issue #8221:
URL: https://github.com/apache/dubbo/issues/8221#issuecomment-873919884


   > 1.在某个controller中 对roleAuthorizationService引用
   > @DubboReference(timeout = 50000)
   > private RoleAuthorizationService roleAuthorizationService;
   > 2. 在另一个controller中 对roleAuthorizationService引用
   > @DubboReference
   > private RoleAuthorizationService roleAuthorizationService;
   
   这两个地方都是初始化Reference并注入field,但是两个注解的属性不相同,提示相同的beanName但不同的属性的错误,因为无法决定使用哪个注解定义的属性来创建reference对象。
   
   正确的用法是:
   (1) Dubbo 3 之后推荐将reference定义和注入分开, 如可以在一个Java Config类中定义reference,在引用的类中通过`@Autowired`来注入接口代理类。
   定义reference:
   ```
   @Configuration
   class ConsumerConfig {
       @Bean
       @DubboReference(timeout = 50000)
       public ReferenceBean<RoleAuthorizationService> roleAuthorizationService (){
             return new ReferenceBean();
       }
   }
   ```
   或者
   ```
   @Configuration
   class ConsumerConfig {
       @DubboReference(timeout = 50000)
       private RoleAuthorizationService roleAuthorizationService;
   }
   ```
   
   
   注入接口:
   ```
   class FooController {
   
       @Autowired
       private RoleAuthorizationService roleAuthorizationService;
      
      ...
   }
   ```
   
   (2) 兼容Dubbo 2的方式,保证所有相同的reference的@DubboReference注解属性相同,那么最终只会创建一个reference对象,不会出现上面的错误。
   上面的例子应该都修改为: 
   ```
   class FooController {
       ...
       @DubboReference(timeout = 50000)
       private RoleAuthorizationService roleAuthorizationService;
       ...
   }
   ```
   ```
   class BarController {
       ...
       @DubboReference(timeout = 50000)
       private RoleAuthorizationService roleAuthorizationService;
       ...
   }
   ```
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org