1. 在springboot中整合RabbitMq的过程中,遇到如下错误:(截取部分)

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'helloSender': Unsatisfied dependency expressed through field 'rabbitTemplate';..........

Caused by: java.lang.IllegalStateException: Failed to introspect Class [org.springframework.amqp.rabbit.core.RabbitTemplate] from ClassLoader [sun.misc.Launcher$AppClassLoader@4e0e2f2a]

Caused by: java.lang.ClassNotFoundException: com.rabbitmq.client.ConfirmCallback

控制台信息:

Unsatisfied dependency expressed through field


出现问题的代码行:


@Autowired     private RabbitTemplate rabbitTemplate;


解决的过程:

(1)猜想应该是RabbitTemplate类没有加载,导致无法注入。看错误信息,要注入RabbitTemplate的bean实体,则是在RabbitAutoConfiguration的内部类中,如下图。

Unsatisfied dependency expressed through field




然后我在启动类中手动加入RabbitAutoConfiguration类,从而加载该bean。即在启动类中显示的注入bean:@EnableAutoConfiguration(exclude={RabbitAutoConfiguration.class})。

Unsatisfied dependency expressed through field


再重新启动。重新启动后报如下错误:Consider defining a bean of type 'org.springframework.amqp.rabbit.core.RabbitTemplate' in your configuration. 即:

Unsatisfied dependency expressed through field


依然没有解决,然后猜想应该不是这个原因。因为类中明明已经加载了该类也有这个bean,但就是不行

最终解决方法:

(2)在网上多次浏览其他类似的错误后,猜想可能是由于包的冲突所造成的。在pom.xml中引入了3.4.1版的amqp-client和spring-boot-starter-amqp,然后点进spring-boot-starter-amqp的包中,发现也引入了amqp-client,其版本是5.4.3。看来是由于包冲突引起的。

将3.4.1版的amqp-client的注释掉,update项目,同时将上面的(1)中启动类的@EnableAutoConfiguration(exclude={RabbitAutoConfiguration.class})也注释。重新再启动,发现不再报错,成功解决。产生该问题的原因就是包冲突了。