dubbo icon indicating copy to clipboard operation
dubbo copied to clipboard

Why <dubbo:referende> or @Reference cannot exist as a Spring Bean to avoid @Autowired errors in the IDEA?

Open yuxiao97 opened this issue 7 years ago • 12 comments

  • [x] I have searched the issues of this repository and believe that this is not a duplicate.
  • [x] I have checked the FAQ of this repository and believe that this is not a duplicate.

Environment

  • Dubbo version: 2.6.0
  • Operating System version: windows 10
  • Java version: 1.8

Steps to reproduce this issue

1:Config dubbo:refrence; 2:@Autowired dubbo:refrence Service in Service

Expected Result

I hope this way above can avoid IDEA can not found xxx Beans errors.

yuxiao97 avatar Mar 27 '19 07:03 yuxiao97

Because of @Reference supports the customized behavior for Dubbo Service, for example:

@Reference
private UserService userService;

@Reference(protocol="dubbo")
private UserService userService;

If the above two fields will be enhanced as the Spring Beans, the problem is how to identify them.

mercyblitz avatar Apr 02 '19 07:04 mercyblitz

Because of @Reference supports the customized behavior for Dubbo Service, for example:

@Reference
private UserService userService;

@Reference(protocol="dubbo")
private UserService userService;

If the above two fields will be enhanced as the Spring Beans, the problem is how to identify them.

OK, I know you means, if as the Spring Bean, Spring can not found and initialize them.

yuxiao97 avatar Apr 03 '19 03:04 yuxiao97

Because of @Reference supports the customized behavior for Dubbo Service, for example:

@Reference
private UserService userService;

@Reference(protocol="dubbo")
private UserService userService;

If the above two fields will be enhanced as the Spring Beans, the problem is how to identify them.

OK, I know you means, if as the Spring Bean, Spring can not found and initialize them.

Thus, Dubbo will provide a new mechanism that @Reference enhances a Spring Bean whose name will be associated with name() attribute, and the Client @Autowired that bean, for example:

@Configuration
public class SomeConfiguration {
    @Reference(name="userService")
    private UserService userService;

    @Reference(name="userService2", protocol="dubbo")
    private UserService userService2;
    ...
}
@RestController
public Class UserController {
   @Autowired
   @Qualifier("userService")
   private UserService userService;

   @Autowired
   @Qualifier("userService2")
   private UserService userService2;
}

mercyblitz avatar Apr 03 '19 07:04 mercyblitz

Because of @Reference supports the customized behavior for Dubbo Service, for example:

@Reference
private UserService userService;

@Reference(protocol="dubbo")
private UserService userService;

If the above two fields will be enhanced as the Spring Beans, the problem is how to identify them.

OK, I know you means, if as the Spring Bean, Spring can not found and initialize them.

Thus, Dubbo will provide a new mechanism that @Reference enhances a Spring Bean whose name will be associated with name() attribute, and the Client @Autowired that bean, for example:

@Configuration
public class SomeConfiguration {
    @Reference(name="userService")
    private UserService userService;

    @Reference(name="userService2", protocol="dubbo")
    private UserService userService2;
    ...
}
@RestController
public Class UserController {
   @Autowired
   @Qualifier("userService")
   private UserService userService;

   @Autowired
   @Qualifier("userService2")
   private UserService userService2;
}

Thanks, expected the function realization!

yuxiao97 avatar Apr 03 '19 14:04 yuxiao97

expect

dhchao11 avatar May 08 '19 03:05 dhchao11

expect

zhizhi555555 avatar May 25 '19 09:05 zhizhi555555

It works since 2.7.3!

mercyblitz avatar Feb 05 '20 05:02 mercyblitz

It works since 2.7.3!

I test on 2.7.7, not worked yet, code like this:

@Configuration
public class SomeConfiguration {
    @DubboReference(version="xxx", protocol="dubbo",check=false)
    private UserService userService;
    ...
}

@Service
public Class OtherService {
   @Autowired
   private UserService userService;
  ...
}

Happened error

Field UserService in OtherService required a bean of type UserService that could not be found.
The injection point has the following annotations:
        - @org.springframework.beans.factory.annotation.Autowired(required=true)

when start server.

yuxiao97 avatar Aug 19 '20 02:08 yuxiao97

It works since 2.7.3!

I test on 2.7.7, not worked yet, code like this:

@Configuration
public class SomeConfiguration {
    @DubboReference(version="xxx", protocol="dubbo",check=false)
    private UserService userService;
    ...
}

@Service
public Class OtherService {
   @Autowired
   private UserService userService;
  ...
}

Happened error

Field UserService in OtherService required a bean of type UserService that could not be found.
The injection point has the following annotations:
        - @org.springframework.beans.factory.annotation.Autowired(required=true)

when start server.

Test on local DEV is OK, local environment Mac OS 10.15.6, jdk8. Server environment CentOS Linux release 7.4.1708 (Core),jdk8

yuxiao97 avatar Aug 19 '20 03:08 yuxiao97

It works since 2.7.3!

I test on 2.7.7, not worked yet, code like this:

@Configuration
public class SomeConfiguration {
    @DubboReference(version="xxx", protocol="dubbo",check=false)
    private UserService userService;
    ...
}

@Service
public Class OtherService {
   @Autowired
   private UserService userService;
  ...
}

Happened error

Field UserService in OtherService required a bean of type UserService that could not be found.
The injection point has the following annotations:
        - @org.springframework.beans.factory.annotation.Autowired(required=true)

when start server.

Test on local DEV is OK, local environment Mac OS 10.15.6, jdk8. Server environment CentOS Linux release 7.4.1708 (Core),jdk8

Same confusing problem. Ok in local, Broken in server.

nickwongwong avatar Mar 08 '21 12:03 nickwongwong

Have a try at this: `@Configuration public class DubboConfig {

@Reference
private DemoServie demoService;

@Bean
public DemoServie demoServie() {
    return demoService;
}   

}`

nickwongwong avatar Mar 08 '21 12:03 nickwongwong

Have a try at this: `@Configuration public class DubboConfig {

@Reference
private DemoServie demoService;

@Bean
public DemoServie demoServie() {
    return demoService;
}   

}`

Yes, I use the same as yours, and you can also use the following methods on 2.7.8

    @DubboReference(protocol = "dubbo", check = false, timeout = 5000, retries = 1)
    public DemoService setDemoService(DemoService demoService){
        return demoService;
    }

yuxiao97 avatar Mar 08 '21 13:03 yuxiao97