org.springframework.jms.connection.CachingConnectionFactory无法强制转换为org.springframework.amqp.rabbit.connection.ConnectionFactory

时间:2013-05-22 03:41:54

标签: spring jms rabbitmq

当我一起运行rabbitmq和jms Spring Project时,我收到以下错误。

  

错误o.s.web.context.ContextLoader - 上下文初始化失败   org.springframework.beans.factory.BeanCreationException:错误   在类路径中定义名为'rabbitTemplate'的bean   资源[com / thys / michels / service_core / amqp / AmqpConfiguration.class]:   bean的实例化失败;嵌套异常是   org.springframework.beans.factory.BeanDefinitionStoreException:   工厂方法[公众   org.springframework.amqp.rabbit.core.RabbitTemplate   com.thys.michels.service_core.amqp.AmqpConfiguration.rabbitTemplate()]   抛出异常;嵌套异常是java.lang.ClassCastException:   org.springframework.jms.connection.CachingConnectionFactory不能   强制转换为org.springframework.amqp.rabbit.connection.ConnectionFactory

     

引起:java.lang.ClassCastException:   org.springframework.jms.connection.CachingConnectionFactory不能   强制转换为org.springframework.amqp.rabbit.connection.ConnectionFactory

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

我的JMS和RabbitMQ类都有一个名为connectionFactory的connectionFactory类,因此JMS的connectionfactory被初始化为RabbitMQ配置。

JMS课程

@Bean
public ConnectionFactory connectionFactory() throws Exception {
            ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory();
            activeMQConnectionFactory.setBrokerURL(environment.getProperty("jms.broker.url"));
            return new CachingConnectionFactory(activeMQConnectionFactory);
        }

RabbitMQ类

@Bean
       public ConnectionFactory connectionFactory() {
                CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();
                cachingConnectionFactory.setUsername(environment.getProperty("amqp.broker.username"));
                cachingConnectionFactory.setPassword(environment.getProperty("amqp.broker.password"));
                cachingConnectionFactory.setHost(environment.getProperty("amqp.broker.host"));
                cachingConnectionFactory.setPort(environment.getProperty( "amqp.broker.port", Integer.class));
                // cachingConnectionFactory.setPort(60705);
                return cachingConnectionFactory;
            }

刚刚更改了connectionFactory名称并且工作正常。