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/08/15 00:51:54 UTC
[GitHub] [dubbo] startjava opened a new issue #8504: 注解与JavaConfig与service group分组写法的迷惑?
startjava opened a new issue #8504:
URL: https://github.com/apache/dubbo/issues/8504
消费者的JavaConfig代码如下:
@Configuration
public class JavaConfigDubbo {
@DubboReference(group = "IServer1_A")
private IService1 iService1;
@DubboReference(group = "IServer1_B")
private IService1 iService2;
}
消费者的Controller代码如下:
@RestController
public class TestCcontroller {
@Autowired
private IService1 service1_1;
@Autowired
private IService1 service1_2;
我的问题是,控制层中的这2个变量数据类型是一样的,如何写能注入指定的实现类呢?
--
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
[GitHub] [dubbo] kylixs edited a comment on issue #8504: 注解与JavaConfig与service group分组写法的迷惑?
Posted by GitBox <gi...@apache.org>.
kylixs edited a comment on issue #8504:
URL: https://github.com/apache/dubbo/issues/8504#issuecomment-900795175
一般来说,当存在某个type的多个候选bean时,可以通过`@Qualifier` 来指定注入的beanName, `@DubboReference`注解处理时默认以fieldName作为beanName(如果是`@Bean`方法的方式,则由Spring创建bean)。
消费者的JavaConfig代码如下:
```java
@Configuration
public class JavaConfigDubbo {
@DubboReference(group = "IServer1_A")
private IService1 iService1;
@DubboReference(group = "IServer1_B")
private IService1 iService2;
}
```
消费者的Controller代码如下:
```java
@RestController
public class TestCcontroller {
@Qualifier("iService1")
@Autowired
private IService1 service1_1;
@Qualifier("iService2")
@Autowired
private IService1 service1_2;
}
```
--
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
[GitHub] [dubbo] chickenlj closed issue #8504: 注解与JavaConfig与service group分组写法的迷惑?
Posted by GitBox <gi...@apache.org>.
chickenlj closed issue #8504:
URL: https://github.com/apache/dubbo/issues/8504
--
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
[GitHub] [dubbo] kylixs commented on issue #8504: 注解与JavaConfig与service group分组写法的迷惑?
Posted by GitBox <gi...@apache.org>.
kylixs commented on issue #8504:
URL: https://github.com/apache/dubbo/issues/8504#issuecomment-900794261
--
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
[GitHub] [dubbo] kylixs edited a comment on issue #8504: 注解与JavaConfig与service group分组写法的迷惑?
Posted by GitBox <gi...@apache.org>.
kylixs edited a comment on issue #8504:
URL: https://github.com/apache/dubbo/issues/8504#issuecomment-900795175
一般来说,当存在某个type的多个候选bean时,可以通过`@Qualifier` 来指定注入的beanName, `@DubboReference`注解处理时默认以fieldName作为beanName(如果是@Bean方法的方式,则由Spring创建bean)。
消费者的JavaConfig代码如下:
```java
@Configuration
public class JavaConfigDubbo {
@DubboReference(group = "IServer1_A")
private IService1 iService1;
@DubboReference(group = "IServer1_B")
private IService1 iService2;
}
```
消费者的Controller代码如下:
```java
@RestController
public class TestCcontroller {
@Qualifier("iService1")
@Autowired
private IService1 service1_1;
@Qualifier("iService2")
@Autowired
private IService1 service1_2;
}
```
--
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
[GitHub] [dubbo] dkisser commented on issue #8504: 注解与JavaConfig与service group分组写法的迷惑?
Posted by GitBox <gi...@apache.org>.
dkisser commented on issue #8504:
URL: https://github.com/apache/dubbo/issues/8504#issuecomment-899482230
https://www.zhihu.com/question/39356740
--
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
[GitHub] [dubbo] kylixs edited a comment on issue #8504: 注解与JavaConfig与service group分组写法的迷惑?
Posted by GitBox <gi...@apache.org>.
kylixs edited a comment on issue #8504:
URL: https://github.com/apache/dubbo/issues/8504#issuecomment-900795175
--
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
[GitHub] [dubbo] startjava commented on issue #8504: 注解与JavaConfig与service group分组写法的迷惑?
Posted by GitBox <gi...@apache.org>.
startjava commented on issue #8504:
URL: https://github.com/apache/dubbo/issues/8504#issuecomment-899592477
感谢,其实写法我能解决,结合@Bean即可。
问题的本意想知道,如何不用@Bean来解决这个问题,以为DUBBO有其它的注解或属性能解决。
--
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
[GitHub] [dubbo] kylixs commented on issue #8504: 注解与JavaConfig与service group分组写法的迷惑?
Posted by GitBox <gi...@apache.org>.
kylixs commented on issue #8504:
URL: https://github.com/apache/dubbo/issues/8504#issuecomment-900795175
一般来说,当存在某个type的多个候选bean时,可以通过`@Qualifier` 来指定注入的beanName。
消费者的JavaConfig代码如下:
```java
@Configuration
public class JavaConfigDubbo {
@DubboReference(group = "IServer1_A")
private IService1 iService1;
@DubboReference(group = "IServer1_B")
private IService1 iService2;
}
```
消费者的Controller代码如下:
```java
@RestController
public class TestCcontroller {
@Qualifier("iService1")
@Autowired
private IService1 service1_1;
@Qualifier("iService2")
@Autowired
private IService1 service1_2;
}
```
--
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
[GitHub] [dubbo] kylixs edited a comment on issue #8504: 注解与JavaConfig与service group分组写法的迷惑?
Posted by GitBox <gi...@apache.org>.
kylixs edited a comment on issue #8504:
URL: https://github.com/apache/dubbo/issues/8504#issuecomment-900795175
一般来说,当存在某个type的多个候选bean时,可以通过`@Qualifier` 来指定注入的beanName, `@DubboReference`注解处理时默认以fieldName作为beanName。
消费者的JavaConfig代码如下:
```java
@Configuration
public class JavaConfigDubbo {
@DubboReference(group = "IServer1_A")
private IService1 iService1;
@DubboReference(group = "IServer1_B")
private IService1 iService2;
}
```
消费者的Controller代码如下:
```java
@RestController
public class TestCcontroller {
@Qualifier("iService1")
@Autowired
private IService1 service1_1;
@Qualifier("iService2")
@Autowired
private IService1 service1_2;
}
```
--
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
[GitHub] [dubbo] kylixs commented on issue #8504: 注解与JavaConfig与service group分组写法的迷惑?
Posted by GitBox <gi...@apache.org>.
kylixs commented on issue #8504:
URL: https://github.com/apache/dubbo/issues/8504#issuecomment-900794261
这里涉及到Spring autowire的实现逻辑,具体可以调试一下代码: org.springframework.beans.factory.support.DefaultListableBeanFactory#determineAutowireCandidate()
如果不指定primary和priority,最后会尝试匹配beanName和fieldName/dependcyName,即如果fieldName和beanName相同就可以成功注入,如果不相同且存在多个候选bean则会报错。
```java
protected String determineAutowireCandidate(Map<String, Object> candidates, DependencyDescriptor descriptor) {
Class<?> requiredType = descriptor.getDependencyType();
String primaryCandidate = determinePrimaryCandidate(candidates, requiredType);
if (primaryCandidate != null) {
return primaryCandidate;
}
String priorityCandidate = determineHighestPriorityCandidate(candidates, requiredType);
if (priorityCandidate != null) {
return priorityCandidate;
}
// Fallback
for (Map.Entry<String, Object> entry : candidates.entrySet()) {
String candidateName = entry.getKey();
Object beanInstance = entry.getValue();
if ((beanInstance != null && this.resolvableDependencies.containsValue(beanInstance)) ||
matchesBeanName(candidateName, descriptor.getDependencyName())) {
return candidateName;
}
}
return null;
}
```
--
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