在bean“容器”中发现的工厂方法不匹配

时间:2018-06-27 10:05:01

标签: java spring spring-amqp

我想在Spring amqp应用程序中实现多个队列侦听器。我尝试过:

    @Bean
    public SimpleMessageListenerContainer container(ConnectionFactory cf, DatabaseTransactionListener listener) {
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(cf);
        container.setQueueNames(QUEUE_DATABASE_TRANSACTION);
        container.setMessageListener(new MessageListenerAdapter(listener, "databaseTransaction"));
        container.setMessageConverter(new SerializerMessageConverter());
        return container;
    }

    @Component
    class DatabaseTransactionListener {

        public TransactionsBean databaseTransaction(TransactionsBean ro) {
            TransactionsBean obj = new TransactionsBean();
            System.out.println("!!!! Performing TransactionsBean processing !!!!");
            obj.setTransaction_id(" ??? some_id: " + ro.getMerchant_id());
            return obj;
        }
    }

    @Bean
    public SimpleMessageListenerContainer container(ConnectionFactory cf, ApiAttemptListener listener) {
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(cf);
        container.setQueueNames(QUEUE_DATABASE_API_ATTEMPT);
        container.setMessageListener(new MessageListenerAdapter(listener, "apiattemptTransaction"));
        container.setMessageConverter(new SerializerMessageConverter());
        return container;
    }

    @Component
    class ApiAttemptListener {

        public ApiAttemptsBean apiattemptTransaction(ApiAttemptsBean ro) {
            ApiAttemptsBean obj = new ApiAttemptsBean();
            System.out.println("!!!! Performing ApiAttempts processing !!!!");
            obj.setMerchant_id(454545);
            return obj;
        }
    }

启动时出现错误:Error creating bean with name 'container' defined in org.database.context.ContextServer: Ambiguous factory method matches found in bean 'container'

实现多个侦听器以处理多个队列的正确方法是什么?

1 个答案:

答案 0 :(得分:2)

Bean名称和Bean工厂方法名称必须唯一。使用@Bean时,默认bean名称是方法名称。

赋予bean工厂方法唯一的名称container1()container2()