dubbo icon indicating copy to clipboard operation
dubbo copied to clipboard

混合使用注解和XML配置导致出现两个application实例(Mixing annotations and XML configuration results in two instances of application)

Open JavaCodeWking opened this issue 3 years ago • 2 comments

Environment

  • Dubbo version: 2.7.16
  • Operating System version: Window 10
  • Java version: 1.8

Steps to reproduce this issue

我并没有使用3.0以上版本,因为和其他的组件兼容性还不够好(I didn't use version 3.0 or above because the compatibility with other components was not good enough)

  1. 我使用@EnableDubbo和@DubboReference注解注入我需要的Spring bean,dubbo的application 和registry信息使用XML 配置,并使用@ImportResource加载此XML配置(I inject Spring beans using @EnableDubbo and @DubboReference, the application and registry information is configured using XML, and I use @ImportResource to load this XML configuration)
  2. 在运行项目后出现此错误(I got this error after running the project):org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'org.apache.dubbo.config.ApplicationConfig' available: expected single matching bean but found 2: demo-consumer,org.apache.dubbo.config.ApplicationConfig#0
  3. 出现了两个application实例(There are two application instances)

我给出实现上述错误的案例代码配置(I give the configuration of the case code that achieves the above error:):

@EnableDubbo @SpringBootApplication @ImportResource(value = "classpath:spring/dubbo-demo-consumer.xml") public class ConsumerApplication { public static void main(String[] args) { SpringApplication.run(ConsumerApplication.class, args); } }

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:property-placeholder/> <dubbo:application name="demo-consumer"/> <dubbo:registry address="127.0.0.1:2181"/> </beans>

@RestController public class TestController { @DubboReference private DemoService demoService; }

JavaCodeWking avatar Jul 24 '22 18:07 JavaCodeWking

  1. @EnableDubbo 注解里默认会注入一个application, 你通过xml手动注入一个application 对象 导致容器中 会存在俩个application对象,且都没设置@Primary注解 导致从spring 中获取的时候不知道拿哪一个
  2. 如果要使用@EnableDubbo 注解 即不需要配置xml,如果想使用dubbo注解和 xml 结合 则 不适用@EnableDubbo注解,在dubboxml中配置 dubbo:annotation/标签来支持注解

liufeiyu1002 avatar Jul 25 '22 05:07 liufeiyu1002

可以尝试一下dubbo.config.mode,具体的值可以参考一下这个 image

wangchengming666 avatar Jul 28 '22 03:07 wangchengming666

我并没有使用3.0以上版本,因为和其他的组件兼容性还不够好(I didn't use version 3.0 or above because the compatibility with other components was not good enough)

这个问题能具体描述一下吗?

chickenlj avatar Sep 02 '22 02:09 chickenlj