SpringBoot org.springframework.beans.factory.UnsatisfiedDependencyException依賴注入異常
報(bào)錯(cuò)信息
最近在學(xué)Spring Boot,今天在做Spring Boot + Mybatis Plus + Vue項(xiàng)目時(shí)啟動(dòng)后端報(bào)錯(cuò):
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'loginController': Unsatisfied dependency expressed through field 'accountMapper';
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'accountMapper' defined in file
[D:\ecas\workspace\ecas\target\classes\com\example\ecas\persistence\AccountMapper.class]: Unsatisfied dependency expressed through bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource
[com/baomidou/mybatisplus/autoconfigure/MybatisPlusAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate
[org.apache.ibatis.session.SqlSessionFactory]: Factory method 'sqlSessionFactory' threw exception; nested exception is java.lang.NoClassDefFoundError: org/mybatis/logging/LoggerFactory
先看報(bào)錯(cuò)信息,說(shuō)出現(xiàn)了一個(gè)依賴注入異常(UnsatisfiedDependencyException),在創(chuàng)建名為 'loginController' 的bean時(shí)出錯(cuò),并且問(wèn)題出現(xiàn)在字段 'accountMapper' 上。
我的loginController里面調(diào)用了AccountMapper和EmailService接口,并且都標(biāo)了@Autowired自動(dòng)注入字段,之前報(bào)過(guò)錯(cuò)拿不到emailService,所以寫(xiě)了一個(gè)setEmailService方法注入了:
// loginController
package com.example.ecas.controller; import com.example.ecas.persistence.AccountMapper; import com.example.ecas.service.SmsService; import com.example.ecas.pojo.Account; import com.example.ecas.service.EmailService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; @RestController @Controller public class LoginController { @Autowired AccountMapper accountMapper; @Autowired private EmailService emailService; @Autowired public void setEmailService(EmailService emailService) { this.emailService = emailService; } // 后面是一些方法,沒(méi)什么問(wèn)題 }
然后是嵌套異常顯示在創(chuàng)建名為 'accountMapper' 的bean時(shí)也遇到了依賴注入異常。 接著,嵌套異常表明在 'sqlSessionFactory' 屬性上設(shè)置bean時(shí)出現(xiàn)了問(wèn)題,這與類路徑中的MybatisPlusAutoConfiguration 類有關(guān)。
最后,嵌套異常顯示在調(diào)用工廠方法 'sqlSessionFactory' 時(shí)出現(xiàn)了異常,具體報(bào)錯(cuò)為 java.lang.NoClassDefFoundError: org/mybatis/logging/LoggerFactory。
大概明白了,掃不到AccountMapper => 創(chuàng)建bean accountMapper出錯(cuò) => loginController出錯(cuò) / 方法sqlSessionFactory出錯(cuò)。
可能原因
同時(shí)在網(wǎng)上查了半天,總結(jié)一下這個(gè)問(wèn)題可能的原因有:
- 相關(guān)依賴導(dǎo)入問(wèn)題。檢查項(xiàng)目的依賴配置文件(如 Maven 的 pom.xml 或 Gradle 的 build.gradle)以確保正確引入了所需的 Spring、MyBatis 和 MyBatis Plus 相關(guān)依賴;
- 依賴版本的兼容性問(wèn)題,不過(guò)這個(gè)IDE應(yīng)該會(huì)標(biāo)紅,總之注意一下;
- 代碼配置問(wèn)題。確保 MyBatis 和 MyBatis Plus 的相關(guān)配置(如數(shù)據(jù)庫(kù)連接信息、Mapper 掃描等)正確無(wú)誤。Controller類上面記得加@Controller注解,調(diào)用的service類要加@Service注解,Mapper層類上要添加@Mapper注解,啟動(dòng)類上包要掃描到loginController類;
- 路徑或者格式不正確問(wèn)題。檢查項(xiàng)目的類路徑中包含所需的類和資源文件。檢查配置文件、Mapper接口以及 MyBatis 和 MyBatis Plus 的其他必要組件是否正確放置在項(xiàng)目的編譯輸出目錄(例如 target/classes)中;
- 緩存或構(gòu)建問(wèn)題。解決辦法就是重新構(gòu)建/清理項(xiàng)目。
我一開(kāi)始檢查了我的注解,都寫(xiě)得好好的(后面發(fā)現(xiàn)有冗余,比如我的loginController上面注解了@RestController又標(biāo)了@Controller,然后AccountMapper上面標(biāo)了@Mapper又@Component,這種情況都只用取前者,不然容易報(bào)錯(cuò),不過(guò)這不是上面錯(cuò)誤的主要原因)
直到我看到第3點(diǎn),嗯?啟動(dòng)類上的包掃描?什么玩意我沒(méi)有啊:
// EcasApplication
package com.example.ecas; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class EcasApplication { public static void main(String[] args) { SpringApplication.run(EcasApplication.class, args); } }
那就加點(diǎn)注解試試看唄,雖然我不會(huì)寫(xiě),但是我可以抄別人的?。?/p>
于是也沒(méi)細(xì)想,自作聰明東拼西湊在Application上面加了一堆注解,如下:
package com.example.ecas; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.context.annotation.ComponentScan; @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, JpaRepositoriesAutoConfiguration.class}) @ComponentScan("com.example.ecas.persistence") public class EcasApplication { public static void main(String[] args) { SpringApplication.run(EcasApplication.class, args); } }
不記得在哪抄了一個(gè)@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, JpaRepositoriesAutoConfiguration.class})
發(fā)現(xiàn)不錯(cuò),報(bào)錯(cuò)消息的格式都變好看了,我應(yīng)該是對(duì)的吧:
***************************
APPLICATION FAILED TO START
***************************
Description:
Field accountMapper in com.example.ecas.controller.LoginController required a bean of type 'com.example.ecas.persistence.AccountMapper' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'com.example.ecas.persistence.AccountMapper' in your configuration.
后來(lái)才知道
@SpringBootApplication注解用于標(biāo)示主應(yīng)用程序類;
exclude參數(shù)用于指定不需要自動(dòng)配置的類;
這里排除的是
DataSourceAutoConfiguration.class
和JpaRepositoriesAutoConfiguration.class,
這意味著在應(yīng)用程序啟動(dòng)過(guò)程中,Spring Boot將不會(huì)自動(dòng)配置數(shù)據(jù)源和JPA存儲(chǔ)庫(kù)相關(guān)的bean。
繼續(xù)看報(bào)錯(cuò)信息,既然他說(shuō)是掃描不到persistence目錄下的AccountMapper,識(shí)別不了bean導(dǎo)致的,那就加個(gè)@ComponentScan("com.example.ecas.persistence")
ok能夠正常運(yùn)行了,測(cè)試一下......發(fā)現(xiàn)能啟動(dòng),但是所有的接口都報(bào)404錯(cuò)誤,后臺(tái)日志沒(méi)有任何反應(yīng)。
看了一下后臺(tái)日志信息,發(fā)現(xiàn)和平時(shí)的不一樣,平時(shí)是這樣的
這次mybatis plus的圖標(biāo)沒(méi)有出來(lái),猜想應(yīng)該和mybatis-plus有關(guān);loginController里所有接口都報(bào)404,推測(cè)是沒(méi)有識(shí)別controller。
于是檢查了pom.xml,發(fā)現(xiàn)既導(dǎo)入了mybatis-plus又導(dǎo)入了mybatis的依賴,所以出現(xiàn)嵌套異常。
把mybatis的依賴注釋掉;
檢查啟動(dòng)類注解,發(fā)現(xiàn)和上面亂寫(xiě)注釋一樣,
EcasApplication應(yīng)該用@MapperScan("com.example.ecas.persistence")注釋
而不是@ComponentScan("com.example.ecas.persistence")注釋
AccountMapper是Mapper類就不要用Component注釋!!
之前加的@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, JpaRepositoriesAutoConfiguration.class})也要把括號(hào)刪掉?。?/p>
package com.example.ecas; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan("com.example.ecas.persistence") public class EcasApplication { public static void main(String[] args) { SpringApplication.run(EcasApplication.class, args); } }
再啟動(dòng),一切ok。
結(jié)論
檢查注釋,不要冗余,什么類該用什么注釋搞清楚;
pom.xml里的依賴mybatis-plus和mybatis不能重復(fù)導(dǎo)入!
到此這篇關(guān)于SpringBoot org.springframework.beans.factory.UnsatisfiedDependencyException依賴注入異常的文章就介紹到這了,更多相關(guān)SpringBoot依賴注入異常內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot Knife4j在線API文檔框架基本使用
knife4j是為Java MVC框架集成Swagger生成Api文檔的增強(qiáng)解決方案,這篇文章主要介紹了SpringBoot中使用Knife4J在線API文檔框架,需要的朋友可以參考下2022-12-12java實(shí)現(xiàn)二維數(shù)組轉(zhuǎn)json的方法示例
這篇文章主要介紹了java實(shí)現(xiàn)二維數(shù)組轉(zhuǎn)json的方法,涉及java數(shù)組遍歷及json格式數(shù)據(jù)構(gòu)造相關(guān)操作技巧,需要的朋友可以參考下2017-10-10Java 使用Docker時(shí)經(jīng)常遇到的五個(gè)問(wèn)題
這篇文章主要介紹了Java 使用Docker時(shí)經(jīng)常遇到的五個(gè)問(wèn)題的相關(guān)資料,需要的朋友可以參考下2016-10-10Logback與Log4j2日志框架性能對(duì)比與調(diào)優(yōu)方式
這篇文章主要介紹了Logback與Log4j2日志框架性能對(duì)比與調(diào)優(yōu)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12Springboot-Management的項(xiàng)目實(shí)踐
本文主要介紹了Springboot-Management的項(xiàng)目實(shí)踐,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05SpringBoot集成Nacos實(shí)現(xiàn)注冊(cè)中心與配置中心流程詳解
這篇文章主要介紹了SpringBoot集成Nacos實(shí)現(xiàn)注冊(cè)中心與配置中心流程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2023-02-02Java找出兩個(gè)大數(shù)據(jù)量List集合中的不同元素的方法總結(jié)
本文將帶大家了解如何快速的找出兩個(gè)相似度非常高的List集合里的不同元素。主要通過(guò)Java API、List集合雙層遍歷比較不同、借助Map集合查找三種方式,需要的可以參考一下2022-10-10Spring MVC整合Shiro權(quán)限控制的方法
這篇文章主要介紹了Spring MVC整合Shiro權(quán)限控制,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05Springboot整合freemarker 404問(wèn)題解決方案
這篇文章主要介紹了Springboot整合freemarker 404問(wèn)題解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05詳解IDEA多module項(xiàng)目maven依賴的一些說(shuō)明
這篇文章主要介紹了詳解IDEA多module項(xiàng)目maven依賴的一些說(shuō)明,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-10-10