SpringBoot org.springframework.beans.factory.UnsatisfiedDependencyException依赖注入异常

 更新时间:2024年02月18日 09:52:30   作者:tomorrow_www  
本文主要介绍了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

先看报错信息,说出现了一个依赖注入异常(UnsatisfiedDependencyException),在创建名为 'loginController' 的bean时出错,并且问题出现在字段 'accountMapper' 上。

我的loginController里面调用了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;
    }

// 后面是一些方法,没什么问题

}

然后是嵌套异常显示在创建名为 'accountMapper' 的bean时也遇到了依赖注入异常。 接着,嵌套异常表明在 'sqlSessionFactory' 属性上设置bean时出现了问题,这与类路径中的MybatisPlusAutoConfiguration 类有关。

最后,嵌套异常显示在调用工厂方法 'sqlSessionFactory' 时出现了异常,具体报错为 java.lang.NoClassDefFoundError: org/mybatis/logging/LoggerFactory。

大概明白了,扫不到AccountMapper => 创建bean accountMapper出错 => loginController出错 / 方法sqlSessionFactory出错。

可能原因

同时在网上查了半天,总结一下这个问题可能的原因有:

  • 相关依赖导入问题。检查项目的依赖配置文件(如 Maven 的 pom.xml 或 Gradle 的 build.gradle)以确保正确引入了所需的 Spring、MyBatis 和 MyBatis Plus 相关依赖;
  • 依赖版本的兼容性问题,不过这个IDE应该会标红,总之注意一下;
  • 代码配置问题。确保 MyBatis 和 MyBatis Plus 的相关配置(如数据库连接信息、Mapper 扫描等)正确无误。Controller类上面记得加@Controller注解,调用的service类要加@Service注解,Mapper层类上要添加@Mapper注解,启动类上包要扫描到loginController类;
  • 路径或者格式不正确问题。检查项目的类路径中包含所需的类和资源文件。检查配置文件、Mapper接口以及 MyBatis 和 MyBatis Plus 的其他必要组件是否正确放置在项目的编译输出目录(例如 target/classes)中;
  • 缓存或构建问题。解决办法就是重新构建/清理项目。

我一开始检查了我的注解,都写得好好的(后面发现有冗余,比如我的loginController上面注解了@RestController又标了@Controller,然后AccountMapper上面标了@Mapper又@Component,这种情况都只用取前者,不然容易报错,不过这不是上面错误的主要原因)

直到我看到第3点,嗯?启动类上的包扫描?什么玩意我没有啊:

// 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})

发现不错,报错消息的格式都变好看了,我应该是对的吧:

***************************
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参数用于指定不需要自动配置的类;

这里排除的是DataSourceAutoConfiguration.classJpaRepositoriesAutoConfiguration.class,

这意味着在应用程序启动过程中,Spring Boot将不会自动配置数据源JPA存储库相关的bean。

继续看报错信息,既然他说是扫描不到persistence目录下的AccountMapper,识别不了bean导致的,那就加个@ComponentScan("com.example.ecas.persistence")

ok能够正常运行了,测试一下......发现能启动,但是所有的接口都报404错误,后台日志没有任何反应。

看了一下后台日志信息,发现和平时的不一样,平时是这样的

这次mybatis plus的图标没有出来,猜想应该和mybatis-plus有关;loginController里所有接口都报404,推测是没有识别controller。

于是检查了pom.xml,发现既导入了mybatis-plus又导入了mybatis的依赖,所以出现嵌套异常。

把mybatis的依赖注释掉;

检查启动类注解,发现和上面乱写注释一样,

EcasApplication应该用@MapperScan("com.example.ecas.persistence")注释

而不是@ComponentScan("com.example.ecas.persistence")注释

AccountMapper是Mapper类就不要用Component注释!!

之前加的@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, JpaRepositoriesAutoConfiguration.class})也要把括号删掉!!

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。

结论

检查注释,不要冗余,什么类该用什么注释搞清楚;

pom.xml里的依赖mybatis-plus和mybatis不能重复导入!

到此这篇关于SpringBoot org.springframework.beans.factory.UnsatisfiedDependencyException依赖注入异常的文章就介绍到这了,更多相关SpringBoot依赖注入异常内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 使用Java实现简单的区块链程序的方法

    使用Java实现简单的区块链程序的方法

    这篇文章主要介绍了使用Java实现简单的区块链程序的方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-04-04
  • 基于selenium 获取新页面元素失败的解决方法

    基于selenium 获取新页面元素失败的解决方法

    今天小编就为大家分享一篇基于selenium 获取新页面元素失败的解决方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-05-05
  • IDEA中的插件导出或者导入插件方式

    IDEA中的插件导出或者导入插件方式

    在无网络环境下安装IDEA插件,需从已安装插件的电脑复制插件目录(路径:IntelliJIdea2022.1/plugins),粘贴至目标电脑对应位置,打开IDEA设置→从磁盘加载插件→选择jar包并应用
    2025-09-09
  • 详细说一说Java自动装箱与拆箱是什么

    详细说一说Java自动装箱与拆箱是什么

    自动装箱与拆箱的机制可以让我们在Java的变量赋值或者是方法调用等情况下使用原始类型或者对象类型更加简单直接,这篇文章主要介绍了Java自动装箱与拆箱是什么的相关资料,需要的朋友可以参考下
    2025-10-10
  • 关于动态参数使用@PathVariable的解析

    关于动态参数使用@PathVariable的解析

    这篇文章主要介绍了关于动态参数使用@PathVariable的解析,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-02-02
  • Java中的ArrayList底层源码分析

    Java中的ArrayList底层源码分析

    这篇文章主要介绍了Java中的ArrayList底层源码分析,通过下标读取元素的速度很快,这是因为ArrayList底层基于数组实现,可以根据下标快速的找到内存地址,接着读取内存地址中存放的数据,需要的朋友可以参考下
    2023-12-12
  • 使用Apache POI在Java中实现Excel单元格的合并

    使用Apache POI在Java中实现Excel单元格的合并

    在日常工作中,Excel是一个不可或缺的工具,尤其是在处理大量数据时,本文将介绍如何使用 Apache POI 库在 Java 中实现 Excel 单元格的合并,需要的可以了解下
    2025-03-03
  • java中关于return返回值的用法详解

    java中关于return返回值的用法详解

    在本篇文章里小编给大家整理的是一篇关于java中关于return返回值的用法详解内容,有兴趣的朋友们可以学习参考下。
    2020-12-12
  • Java+WebSocket实现简单实时双人协同pk答题系统

    Java+WebSocket实现简单实时双人协同pk答题系统

    在实时互动应用中,实现流畅的多人协同对战功能是一大挑战,WebSocket技术,以其全双工通信能力,提供了解决方案,本文我们就来使用WebSocket实现简单实时双人协同pk答题系统吧
    2025-06-06
  • Spring Boot DevTools 全局配置学习指南

    Spring Boot DevTools 全局配置学习指南

    这篇文章主要介绍了Spring Boot DevTools 全局配置,注意包括直接重启项目与devtools重启的区别,DevTools配置,DevTools全局配置及trigger-file控制重启行为的相关知识,需要的朋友可以参考下
    2022-03-03

最新评论