2015-02-23 136 views
0

我不知道爲什麼Method「connectionFactory」返回JedisConnecionFactory的一個實例,它應該不是org.springframework.amqp.rabbit.connection.ConnectionFactory?java.lang.ClassCastException:JedisConnectionFactory不能轉換爲ConnectionFactory

線以下異常 「新RabbitTemplate(connectionFactory的())」(見下面的代碼)

Feb 23, 2015 11:50:20 AM org.apache.catalina.core.StandardContext startInternal 
SEVERE: Error listenerStart 
r:4.1.5.RELEASE] 
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106) [spring-web-4.1.5.RELEASE.jar:4.1.5.RELEASE] 
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4887) [catalina.jar:7.0.39] 
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5381) [catalina.jar:7.0.39] 
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) [catalina.jar:7.0.39] 
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559) [catalina.jar:7.0.39] 
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549) [catalina.jar:7.0.39] 
    at java.util.concurrent.FutureTask.run(FutureTask.java:266) [na:1.8.0] 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0] 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0] 
    at java.lang.Thread.run(Thread.java:744) [na:1.8.0] 
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.queue.MessageQueueSender]: Factory method 'messageQueueSender' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'rabbitTemplate' defined in class path resource [com/config/MessageQueueConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.amqp.rabbit.core.RabbitTemplate]: Factory method 'rabbitTemplate' threw exception; nested exception is java.lang.ClassCastException: org.springframework.data.redis.connection.jedis.JedisConnectionFactory cannot be cast to org.springframework.amqp.rabbit.connection.ConnectionFactory 
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189) ~[spring-beans-4.1.5.RELEASE.jar:4.1.5.RELEASE] 
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588) ~[spring-beans-4.1.5.RELEASE.jar:4.1.5.RELEASE] 
    ... 23 common frames omitted 
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'rabbitTemplate' defined in class path resource [com/config/MessageQueueConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.amqp.rabbit.core.RabbitTemplate]: Factory method 'rabbitTemplate' threw exception; nested exception is java.lang.ClassCastException: org.springframework.data.redis.connection.jedis.JedisConnectionFactory cannot be cast to org.springframework.amqp.rabbit.connection.ConnectionFactory 
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599) ~[spring-beans-4.1.5.RELEASE.jar:4.1.5.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1111) ~[spring-beans-4.1.5.RELEASE.jar:4.1.5.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1006) ~[spring-beans-4.1.5.RELEASE.jar:4.1.5.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504) ~[spring-beans-4.1.5.RELEASE.jar:4.1.5.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) ~[spring-beans-4.1.5.RELEASE.jar:4.1.5.RELEASE] 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303) ~[spring-beans-4.1.5.RELEASE.jar:4.1.5.RELEASE] 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.1.5.RELEASE.jar:4.1.5.RELEASE] 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299) ~[spring-beans-4.1.5.RELEASE.jar:4.1.5.RELEASE] 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) ~[spring-beans-4.1.5.RELEASE.jar:4.1.5.RELEASE] 
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:322) ~[spring-context-4.1.5.RELEASE.jar:4.1.5.RELEASE] 
    at com.config.MessageQueueConfig$$EnhancerBySpringCGLIB$$68caa0b1.rabbitTemplate(<generated>) ~[spring-core-4.1.5.RELEASE.jar:na] 
    at com.config.MessageQueueConfig.messageQueueSender(MessageQueueConfig.java:61) ~[MessageQueueConfig.class:na] 
    at com.config.MessageQueueConfig$$EnhancerBySpringCGLIB$$68caa0b1.CGLIB$messageQueueSender$1(<generated>) ~[spring-core-4.1.5.RELEASE.jar:na] 
    at com.config.MessageQueueConfig$$EnhancerBySpringCGLIB$$68caa0b1$$FastClassBySpringCGLIB$$99208f13.invoke(<generated>) ~[spring-core-4.1.5.RELEASE.jar:na] 
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) ~[spring-core-4.1.5.RELEASE.jar:4.1.5.RELEASE] 
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:309) ~[spring-context-4.1.5.RELEASE.jar:4.1.5.RELEASE] 
    at com.config.MessageQueueConfig$$EnhancerBySpringCGLIB$$68caa0b1.messageQueueSender(<generated>) ~[spring-core-4.1.5.RELEASE.jar:na] 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0] 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0] 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0] 
    at java.lang.reflect.Method.invoke(Method.java:483) ~[na:1.8.0] 
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162) ~[spring-beans-4.1.5.RELEASE.jar:4.1.5.RELEASE] 
    ... 24 common frames omitted 
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.amqp.rabbit.core.RabbitTemplate]: Factory method 'rabbitTemplate' threw exception; nested exception is java.lang.ClassCastException: org.springframework.data.redis.connection.jedis.JedisConnectionFactory cannot be cast to org.springframework.amqp.rabbit.connection.ConnectionFactory 
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:189) ~[spring-beans-4.1.5.RELEASE.jar:4.1.5.RELEASE] 
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:588) ~[spring-beans-4.1.5.RELEASE.jar:4.1.5.RELEASE] 
    ... 45 common frames omitted 
Caused by: java.lang.ClassCastException: org.springframework.data.redis.connection.jedis.JedisConnectionFactory cannot be cast to org.springframework.amqp.rabbit.connection.ConnectionFactory 
    at com.config.MessageQueueConfig$$EnhancerBySpringCGLIB$$68caa0b1.connectionFactory(<generated>) ~[spring-core-4.1.5.RELEASE.jar:na] 
    at com.config.MessageQueueConfig.rabbitTemplate(MessageQueueConfig.java:75) ~[MessageQueueConfig.class:na] 
    at com.config.MessageQueueConfig$$EnhancerBySpringCGLIB$$68caa0b1.CGLIB$rabbitTemplate$2(<generated>) ~[spring-core-4.1.5.RELEASE.jar:na] 
    at com.config.MessageQueueConfig$$EnhancerBySpringCGLIB$$68caa0b1$$FastClassBySpringCGLIB$$99208f13.invoke(<generated>) ~[spring-core-4.1.5.RELEASE.jar:na] 
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) ~[spring-core-4.1.5.RELEASE.jar:4.1.5.RELEASE] 
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:309) ~[spring-context-4.1.5.RELEASE.jar:4.1.5.RELEASE] 
    at com.config.MessageQueueConfig$$EnhancerBySpringCGLIB$$68caa0b1.rabbitTemplate(<generated>) ~[spring-core-4.1.5.RELEASE.jar:na] 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0] 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0] 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0] 
    at java.lang.reflect.Method.invoke(Method.java:483) ~[na:1.8.0] 
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:162) ~[spring-beans-4.1.5.RELEASE.jar:4.1.5.RELEASE] 
    ... 46 common frames omitted 

@Configuation類

import org.springframework.amqp.core.AmqpAdmin; 
import org.springframework.amqp.core.Queue; 
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory; 
import org.springframework.amqp.rabbit.connection.ConnectionFactory; 
import org.springframework.amqp.rabbit.core.RabbitAdmin; 
import org.springframework.amqp.rabbit.core.RabbitTemplate; 
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer; 
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter; 
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.beans.factory.annotation.Value; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.ComponentScan; 
import org.springframework.context.annotation.Configuration; 

@Configuration 
@ComponentScan(basePackages={"..."}) 
public class MessageQueueConfig { 
    .. 
    @Bean 
    public ConnectionFactory connectionFactory() { 
     CachingConnectionFactory connectionFactory = new CachingConnectionFactory(url); 
     connectionFactory.setUsername(username); 
     connectionFactory.setPassword(password); 
     connectionFactory.setPort(port); 
     return connectionFactory; 
    } 

    @Bean 
    public RabbitTemplate rabbitTemplate() { 
     RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory()); 
     rabbitTemplate.setConnectionFactory(connectionFactory()); 
     rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter()); 
     rabbitTemplate.setRoutingKey("message.queue"); 
     return rabbitTemplate; 
    } 


} 

回答

1

@ComponentScan必須尋找其他配置文件用redis連接工廠bean與覆蓋這個相同的名稱。

您可以使用DEBUG日誌記錄org.springframework;它在解析bean時會發出大量日誌。

或者,只需更改您的bean定義以使用不同的bean名稱:rabbitConnectionFactory()

相關問題