RabbitMQ的org.springframework.beans.factory.NoUniqueBeanDefinitionException问题

时间:2015-11-16 05:48:51

标签: spring spring-boot spring-rabbit

我正在使用spring boot构建一个项目,我希望在我的项目中整合RabbitMQ。我正在使用spring boot rabbit mq。我想创建两个rabbitmq模板实例,为此我已经给出了这个配置

package com.example.test;

import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.AbstractRoutingConnectionFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.SimpleRoutingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by mohdqasim on 12/11/15.
 */
@Configuration
public class ConnectionFactory extends AbstractRoutingConnectionFactory{

    Map<Object, org.springframework.amqp.rabbit.connection.ConnectionFactory> factories = new HashMap<>(2);

    public static final String EXTERNAL = "external";
    public static final String INTERNAL = "internal";


    public ConnectionFactory(){
        org.springframework.amqp.rabbit.connection.ConnectionFactory connectionFactory = new CachingConnectionFactory("mq.server-one.com");
        factories.put(INTERNAL, connectionFactory);

        connectionFactory = new CachingConnectionFactory("extmq.server-two.com");
        factories.put(EXTERNAL, connectionFactory);
    }

    @Override
    protected Object determineCurrentLookupKey() {
        return null;
    }

    @Bean(name=EXTERNAL)
    public RabbitTemplate rabbitTemplate1() {
        return new RabbitTemplate(factories.get(EXTERNAL));
    }

    @Bean(name=INTERNAL)
    public RabbitTemplate rabbitTemplate2() {
        return new RabbitTemplate(factories.get(INTERNAL));
    }

}

我已经创建了一个我已经自动装配rabbittemplate

的控制器
package com.example.test;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * Created by mohdqasim on 12/11/15.
 */
@RestController
@RequestMapping("/api/v1")
public class MyController {




    @Autowired
    @Qualifier(ConnectionFactory.EXTERNAL)
    RabbitTemplate rt1;

    /*@Autowired
    @Qualifier(ConnectionFactory.INTERNAL)
    RabbitTemplate rt2;*/

    /*@Autowired
    @Qualifier("rabbitTemplate2")
    RabbitTemplate rt2;*/

    @RequestMapping("/data")
    public String getData(){
        System.out.println("Hellow Qasim");
        rt1.convertAndSend("spring-boot-internal","Testing internal queue 1 with spring boot");
//        rt2.convertAndSend("spring-boot-external","Testing external queue 2 with spring boot");
//        connection1RabbitTemplate.convertAndSend("spring-boot2","Testing queue 2 with spring boot");
        return "data";
    }
}

在我的pom.xml文件中,我为rabbitmq

添加了这个依赖项
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
    <version>1.2.7.RELEASE</version>
</dependency>

但是当我开始我的项目时,它会抛出这个错误:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'rabbitMessagingTemplate' defined in class path resource [org/springframework/boot/autoconfigure/amqp/RabbitAutoConfiguration$MessagingTemplateConfiguration.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [org.springframework.amqp.rabbit.core.RabbitTemplate]: : No qualifying bean of type [org.springframework.amqp.rabbit.core.RabbitTemplate] is defined: expected single matching bean but found 2: external,internal; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [org.springframework.amqp.rabbit.core.RabbitTemplate] is defined: expected single matching bean but found 2: external,internal
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:464)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1117)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1012)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:759)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:117)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:689)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:321)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:969)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:958)
    at com.example.RabbitMqDemoApplication.main(RabbitMqDemoApplication.java:15)
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [org.springframework.amqp.rabbit.core.RabbitTemplate] is defined: expected single matching bean but found 2: external,internal
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1054)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:813)
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741)

1 个答案:

答案 0 :(得分:2)

看起来问题不在您的代码中,而是在其他不使用@Qualifier@Named的地方。您可以尝试将@Primary放在其中一个bean上,否则您需要禁用Spring Boot AMPQ的自动配置。

参考:http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/annotation/Primary.html