欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot org.springframework.beans.factory.UnsatisfiedDependencyException依賴注入異常

 更新時(shí)間:2024年02月18日 09:52:30   作者:tomorrow_www  
本文主要介紹了SpringBoot org.springframework.beans.factory.UnsatisfiedDependencyException依賴注入異常,文中通過(guò)示例代碼介紹的很詳細(xì),具有一定的參考價(jià)值,感興趣的可以了解一下

報(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.classJpaRepositoriesAutoConfiguration.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)文章

最新評(píng)論