apache / dubbo

The java implementation of Apache Dubbo. An RPC and microservice framework.
https://dubbo.apache.org/
Apache License 2.0
40.4k stars 26.42k forks source link

How to use @DubboReference annotations Like xml and the basic attributes are only configured once? #6611

Closed yuxiao97 closed 3 years ago

yuxiao97 commented 4 years ago

Environment

Steps to reproduce this issue

How to use @DubboReference annotations Like xml, the basic attributes are only configured once, instead of configuring the same attributes for each reference.

yuxiao97 commented 4 years ago

I reference dubbo service in same JavaConfig class, then @Autowired in used class, but in used class tips no beans found of the type, even I use interfaceName in @DubboReference and use @Qualifier, the same result with before.

yuxiao97 commented 4 years ago

I reference dubbo service in same JavaConfig class, then @Autowired in used class, but in used class tips no beans found of the type, even I use interfaceName in @DubboReference and use @Qualifier, the same result with before.

When use JavaConfig then start failed:

Field AAAService in BBBServiceImpl required a bean of type 'AAAService' that could not be found.
The injection point has the following annotations:
        - @org.springframework.beans.factory.annotation.Autowired(required=true)
JasonMing commented 3 years ago

This is an annoying behavior.

Since 2.7.5 (maybe), @DubboReference cache the generated stub and register into spring application context.

But, if you do this in the @Configuration class, will break the dependency graph, because there is no @Bean method to tell spring should initialize the java config class before your service class.

To solve this problem, temporary, should put a @DependsOn("javaConfigClassBeanName") onto you service class to fix the broken dependency graph.

However, this solution is not elegant enough. The best way is separate dubbo reference declaration and injection.

yuxiao97 commented 3 years ago

I use JavaConfig as SpringBean, than use @DubboReference reference Dubbo service and declcare Dubbo service as Bean, like this:

@Configuration
public class DubboServiceReferenceConfig {

    @DubboReference(version = "${dubbo.application.version}", check = false)
    private UserInfoService userInfoService;

    @Bean
    public UserInfoService userInfoService() {
        return userInfoService;
    }

}