当前位置: 首页 > news >正文

报错 | Spring报错详解

Spring报错详解

  • 一、前言
  • 二、报错提示
  • 三、分层解读
    • 1.最下面一层Caused by
    • 2.上一层Caused by
    • 3.最上层Caused by
  • 四、总结
  • 五、解决方案

一、前言

本文主要是记录在初次学习Spring时遇到报错后的解读以及解决方案

二、报错提示

在这里插入图片描述

三、分层解读

遇到报错的时候,我们需要从下往上阅读错误,从最下面一层的Caused by开始阅读,最核心的错误是在最下面一层的;最上面 Exception in....是对下面的错误的包装

1.最下面一层Caused by

在这里插入图片描述

Caused by: java.lang.NoSuchMethodException: com.itheima.dao.impl.BookDaoImpl.

NoSuchMethodException:没有这样的方法导致的异常

在这句话后面列出了一个方法:com.itheima.dao.impl.BookDaoImpl.<init>(),也就是缺失了这个方法导致的异常报错

2.上一层Caused by

在这里插入图片描述

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.itheima.dao.impl.BookDaoImpl]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.itheima.dao.impl.BookDaoImpl.<init>()

BeanInstantiationException:Bean实例化异常

Failed to instantiate:实例化失败,在中国报错后面给了一个类com.itheima.dao.impl.BookDaoImpl,表示这个类实例化失败

No default constructor found:未找到默认构造函数

nested:嵌套,嵌套下一层的报错

3.最上层Caused by

在这里插入图片描述

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bookDao' defined in class path resource [applicationContext.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.itheima.dao.impl.BookDaoImpl]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.itheima.dao.impl.BookDaoImpl.

这里我们只要看nested前的报错信息即可

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bookDao' defined in class path resource [applicationContext.xml]: Instantiation of bean failed

BeanCreationException:创建Bean异常:后面写出创建哪一个Bean失败了

Error creating bean with name 'bookDao' defined in class path resource [applicationContext.xml]:在applicationContext.xml文件里名字为bookDaoBean创建失败

Instantiation of bean failed:Bean实例化失败

四、总结

这个报错原因很清晰:在 com.itheima.dao.impl.BookDaoImpl这个类中缺少默认构造函数,这里给出 com.itheima.dao.impl.BookDaoImpl类的代码


public class BookDaoImpl implements BookDao {public BookDaoImpl(int i) {System.out.println("book dao constructor is running ....");}public void save() {System.out.println("book dao save ...");}}

可以看出这里缺少不含参数的构造函数public BookDaoImpl()

五、解决方案

  1. 在原有代码上加上如下代码

    public BookDaoImpl(){}
    
  2. 修改含参构造方法,去掉int i

    public BookDaoImpl(){}
    
http://www.lryc.cn/news/118781.html

相关文章:

  • PHP最简单自定义自己的框架数据库封装调用(五)
  • 使用Redis来实现点赞功能的基本思路
  • 【黑马头条之app端文章搜索ES-MongoDB】
  • Nginx安装以及LVS-DR集群搭建
  • 后端开发9.商品类型模块
  • spring框架自带的http工具RestTemplate用法
  • 【flink】Checkpoint expired before completing.
  • 【论文阅读】NoDoze:使用自动来源分类对抗威胁警报疲劳(NDSS-2019)
  • 【ARM64 常见汇编指令学习 16 -- ARM64 SMC 指令】
  • uprobe trace多线程mutex等待耗时
  • Linux 和 MacOS 中的 profile 文件详解(一)
  • 不用技术代码,如何制作成绩查询系统?
  • flinksql sink to sr often fail because of nullpoint
  • 达梦数据库:Error updating database. Cause: dm.jdbc.driver.DMException: 数据未找到
  • 电脑怎么查看连接过的WIFI密码(测试环境win11,win10也能用)
  • 处理数据部分必备代码
  • layui 集成 ztree异步加载
  • LeetCode面向运气之Javascript—第27题-移除元素-98.93%
  • 谷歌云 | 电子商务 | 如何更好地管理客户身份以支持最佳的用户体验
  • 行业追踪,2023-08-09
  • 视图、存储过程、函数、触发器
  • 数学建模学习(10):遗传算法
  • 私域流量整合:社群裂变的综合策略
  • Redis的RDB持久化
  • 三、MySql表的操作
  • 【模型加速部署】—— Pytorch自动混合精度训练
  • 【Qt】信号槽的三种连接方式
  • Jtti:Ubuntu静态IP地址怎么配置
  • iconfont 使用
  • 基于java冰雪旅游服务网设计与实现