New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
BeanNotOfRequiredTypeException when using spring bean injection #12637
Comments
可以提供一个能够复现异常的 |
demo,这是我复现的demo,Demo2Controller中的demo1Service会导致异常,修改属性名为demo2Service就没问题。 |
同理 不使用dubbo 注解的时候 如果 demo1Service,demo2Service实现声明为
同时 按照
注入也会类型异常 |
这个逻辑能理解,但我还是更喜欢2.x那样不会根据属性名去注册别名,这样2.x到3.x升级也会更方便。谢谢你的解答!!! |
Related with #12474 |
可以试一下 3.2.3 版本是不是还有问题 |
如果不同类型的成员变量使用相同的属性命名,在3.2.3 版本的异常信息变得准确了。
|
@AlbumenJ Hi, I have reproduced this bug in both Dubbo 3.2.2 & 3.2.3, so I'm guessing it's still pending to be fixed. If so, I am interested in solving this issue, please assign it to me :) |
I think I have found what went wrong after finishing several breakpoint tests. Currently, dubbo will register the instance (of the field that is annotated with @DubboReference) into the IoC container, and it uses the field's name as the bean key by default. For example:
In the above code, dubbo will register "testService" --> [TestService bean] into the IoC container. This usually works fine, but not for the case when some other places in the project use some code like this:
As @liufeiyu1002 said, @ Resource will enable Spring to look for a bean instance in the IoC container using the field name. Which means, Spring will detect the existence of "testService" --> [TestService bean], find out that TestService (existed bean's class) and Test2Service (the class of the field currently being injected) do not match. That is where an exception is thrown. |
ExperimentTo try and find a way to fix this issue, I did an experiment. In the following code, the two fields have difference classes but same field names, and they both use @DubboReference for the dependency injection.
And dubbo can handle this situation. The first generated bean (assuming it's TestService) is "testService" -> [TestService bean], and the second generated bean is "testService#2" -> [Test2Service bean]. Dubbo slightly changes the latter's bean key to avoid conflicts. My idea to solve this problemSo basically, my idea to resolve this issue is that: when we register @DubboReference-annotated bean into Spring IoC container, we shall attach the serial number after its bean key (no matter if it's the first time or not). Applying such an idea to the above code, it will be "testService#1" -> [TestService bean] and "testService#2" -> [Test2Service bean]. Since I am new to the community, I may have overlooked certain important details. Please kindly share your opinions on whether this solution is workable, thanks! |
对比了下2.7.X与3.X org.apache.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor#registerReferenceBean 实现 |
前面也讨论过这个问题
3.X ReferenceAnnotationBeanPostProcessor 按@DubboReference属性名注册别名
这样会导致在不同的类中注入不同类型的Bean时必须要用不同的属性名称,如果像下面这样就会导致异常
dubbo2.x 就没有这个问题,3.0中使用属性名注册别名是有什么特殊的用意吗?
我理解这与spring并没有关系 https://github.com/apache/dubbo/issues/10443#issuecomment-1212703407,
因为我只要不用 dubbo3.x 就不会导致异常。
@chickenlj
The text was updated successfully, but these errors were encountered: