SpringBoot org.springframework.beans.factory.UnsatisfiedDependencyException依賴注入異常
報錯信息
最近在學Spring Boot,今天在做Spring Boot + Mybatis Plus + Vue項目時啟動后端報錯:
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
先看報錯信息,說出現(xiàn)了一個依賴注入異常(UnsatisfiedDependencyException),在創(chuàng)建名為 'loginController' 的bean時出錯,并且問題出現(xiàn)在字段 'accountMapper' 上。
我的loginController里面調(diào)用了AccountMapper和EmailService接口,并且都標了@Autowired自動注入字段,之前報過錯拿不到emailService,所以寫了一個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; } // 后面是一些方法,沒什么問題 }
然后是嵌套異常顯示在創(chuàng)建名為 'accountMapper' 的bean時也遇到了依賴注入異常。 接著,嵌套異常表明在 'sqlSessionFactory' 屬性上設置bean時出現(xiàn)了問題,這與類路徑中的MybatisPlusAutoConfiguration 類有關。
最后,嵌套異常顯示在調(diào)用工廠方法 'sqlSessionFactory' 時出現(xiàn)了異常,具體報錯為 java.lang.NoClassDefFoundError: org/mybatis/logging/LoggerFactory。
大概明白了,掃不到AccountMapper => 創(chuàng)建bean accountMapper出錯 => loginController出錯 / 方法sqlSessionFactory出錯。
可能原因
同時在網(wǎng)上查了半天,總結(jié)一下這個問題可能的原因有:
- 相關依賴導入問題。檢查項目的依賴配置文件(如 Maven 的 pom.xml 或 Gradle 的 build.gradle)以確保正確引入了所需的 Spring、MyBatis 和 MyBatis Plus 相關依賴;
- 依賴版本的兼容性問題,不過這個IDE應該會標紅,總之注意一下;
- 代碼配置問題。確保 MyBatis 和 MyBatis Plus 的相關配置(如數(shù)據(jù)庫連接信息、Mapper 掃描等)正確無誤。Controller類上面記得加@Controller注解,調(diào)用的service類要加@Service注解,Mapper層類上要添加@Mapper注解,啟動類上包要掃描到loginController類;
- 路徑或者格式不正確問題。檢查項目的類路徑中包含所需的類和資源文件。檢查配置文件、Mapper接口以及 MyBatis 和 MyBatis Plus 的其他必要組件是否正確放置在項目的編譯輸出目錄(例如 target/classes)中;
- 緩存或構(gòu)建問題。解決辦法就是重新構(gòu)建/清理項目。
我一開始檢查了我的注解,都寫得好好的(后面發(fā)現(xiàn)有冗余,比如我的loginController上面注解了@RestController又標了@Controller,然后AccountMapper上面標了@Mapper又@Component,這種情況都只用取前者,不然容易報錯,不過這不是上面錯誤的主要原因)
直到我看到第3點,嗯?啟動類上的包掃描?什么玩意我沒有?。?/p>
// 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); } }
那就加點注解試試看唄,雖然我不會寫,但是我可以抄別人的??!
于是也沒細想,自作聰明東拼西湊在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); } }
不記得在哪抄了一個@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, JpaRepositoriesAutoConfiguration.class})
發(fā)現(xiàn)不錯,報錯消息的格式都變好看了,我應該是對的吧:
***************************
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.
后來才知道
@SpringBootApplication注解用于標示主應用程序類;
exclude參數(shù)用于指定不需要自動配置的類;
這里排除的是
DataSourceAutoConfiguration.class
和JpaRepositoriesAutoConfiguration.class,
這意味著在應用程序啟動過程中,Spring Boot將不會自動配置數(shù)據(jù)源和JPA存儲庫相關的bean。
繼續(xù)看報錯信息,既然他說是掃描不到persistence目錄下的AccountMapper,識別不了bean導致的,那就加個@ComponentScan("com.example.ecas.persistence")
ok能夠正常運行了,測試一下......發(fā)現(xiàn)能啟動,但是所有的接口都報404錯誤,后臺日志沒有任何反應。
看了一下后臺日志信息,發(fā)現(xiàn)和平時的不一樣,平時是這樣的
這次mybatis plus的圖標沒有出來,猜想應該和mybatis-plus有關;loginController里所有接口都報404,推測是沒有識別controller。
于是檢查了pom.xml,發(fā)現(xiàn)既導入了mybatis-plus又導入了mybatis的依賴,所以出現(xiàn)嵌套異常。
把mybatis的依賴注釋掉;
檢查啟動類注解,發(fā)現(xiàn)和上面亂寫注釋一樣,
EcasApplication應該用@MapperScan("com.example.ecas.persistence")注釋
而不是@ComponentScan("com.example.ecas.persistence")注釋
AccountMapper是Mapper類就不要用Component注釋?。?/p>
之前加的@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, JpaRepositoriesAutoConfiguration.class})也要把括號刪掉?。?/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); } }
再啟動,一切ok。
結(jié)論
檢查注釋,不要冗余,什么類該用什么注釋搞清楚;
pom.xml里的依賴mybatis-plus和mybatis不能重復導入!
到此這篇關于SpringBoot org.springframework.beans.factory.UnsatisfiedDependencyException依賴注入異常的文章就介紹到這了,更多相關SpringBoot依賴注入異常內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringBoot Knife4j在線API文檔框架基本使用
knife4j是為Java MVC框架集成Swagger生成Api文檔的增強解決方案,這篇文章主要介紹了SpringBoot中使用Knife4J在線API文檔框架,需要的朋友可以參考下2022-12-12java實現(xiàn)二維數(shù)組轉(zhuǎn)json的方法示例
這篇文章主要介紹了java實現(xiàn)二維數(shù)組轉(zhuǎn)json的方法,涉及java數(shù)組遍歷及json格式數(shù)據(jù)構(gòu)造相關操作技巧,需要的朋友可以參考下2017-10-10Logback與Log4j2日志框架性能對比與調(diào)優(yōu)方式
這篇文章主要介紹了Logback與Log4j2日志框架性能對比與調(diào)優(yōu)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12SpringBoot集成Nacos實現(xiàn)注冊中心與配置中心流程詳解
這篇文章主要介紹了SpringBoot集成Nacos實現(xiàn)注冊中心與配置中心流程,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧2023-02-02Java找出兩個大數(shù)據(jù)量List集合中的不同元素的方法總結(jié)
本文將帶大家了解如何快速的找出兩個相似度非常高的List集合里的不同元素。主要通過Java API、List集合雙層遍歷比較不同、借助Map集合查找三種方式,需要的可以參考一下2022-10-10Springboot整合freemarker 404問題解決方案
這篇文章主要介紹了Springboot整合freemarker 404問題解決方案,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-05-05