Unsatisfied dependency expressed through field 'rabbitTemplate'錯誤總結


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

控制台信息:

     

 

出現問題的代碼行:

    @Autowired
    private RabbitTemplate rabbitTemplate;

解決的過程:

(1)猜想應該是RabbitTemplate類沒有加載,導致無法注入。看錯誤信息,要注入RabbitTemplate的bean實體,則是在RabbitAutoConfiguration的內部類中,如下圖。

         

 

 

 

然后我在啟動類中手動加入RabbitAutoConfiguration類,從而加載該bean。即在啟動類中顯示的注入bean:@EnableAutoConfiguration(exclude={RabbitAutoConfiguration.class})。

       

 

 再重新啟動。重新啟動后報如下錯誤:Consider defining a bean of type 'org.springframework.amqp.rabbit.core.RabbitTemplate' in your configuration. 即:

     

 

 依然沒有解決,然后猜想應該不是這個原因。因為類中明明已經加載了該類也有這個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})也注釋。重新再啟動,發現不再報錯,成功解決。產生該問題的原因就是包沖突了。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM