Caused by: : Error creating bean with name ‘s

时间:2025-03-23 12:18:40

前言

        在练习一个springboot项目时,在执行时出现了下面的报错信息,找了好久都不知道怎么回事,因此,记录下整个过程,从而鞭策如菜鸡一样的自己。我的问题是打包问题(删除pom打包方式就可以了)

目  录

前言

1、报错问题

2、解决过程

3、解决方式

4、总结


1、报错问题

Caused by: : Error creating bean with name 'sqlSessionFactory' defined in class path resource [com/baomidou/mybatisplus/autoconfigure/]: Bean instantiation via factory method failed; nested exception is : Failed to instantiate []: Factory method 'sqlSessionFactory' threw exception; nested exception is : Error: GlobalConfigUtils setMetaData Fail !  Cause:: Error: Get proxy targetObject exception !  Cause:: Error creating bean with name '' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$]: Bean instantiation via factory method failed; nested exception is : Failed to instantiate []: Factory method 'dataSource' threw exception; nested exception is $DataSourceBeanCreationException: Failed to determine a suitable driver class

2、解决过程

        整体的错误为:

: Error creating bean with name 'eduTeacherController': Unsatisfied dependency expressed through field 'teacherService'; nested exception is : Error creating bean with name 'eduTeacherServiceImpl': Unsatisfied dependency expressed through field 'baseMapper'; nested exception is : Error creating bean with name 'eduTeacherMapper' defined in file [D:\ajavaproject\guli-parent\service\service-edu\target\classes\com\atguigu\eduservice\mapper\]: Unsatisfied dependency expressed through bean property 'sqlSessionFactory'; nested exception is : Error creating bean with name 'sqlSessionFactory' defined in class path resource [com/baomidou/mybatisplus/autoconfigure/]: Bean instantiation via factory method failed; nested exception is : Failed to instantiate []: Factory method 'sqlSessionFactory' threw exception; nested exception is : Error: GlobalConfigUtils setMetaData Fail !  Cause:: Error: Get proxy targetObject exception !  Cause:: Error creating bean with name '' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$]: Bean instantiation via factory method failed; nested exception is : Failed to instantiate []: Factory method 'dataSource' threw exception; nested exception is $DataSourceBeanCreationException: Failed to determine a suitable driver class
	at $(:639) ~[spring-beans-5.2.:5.2.]
	at (:116) ~[spring-beans-5.2.:5.2.]
	at (:397) ~[spring-beans-5.2.:5.2.]
	at (:1429) ~[spring-beans-5.2.:5.2.]
	at (:594) ~[spring-beans-5.2.:5.2.]
	at (:517) ~[spring-beans-5.2.:5.2.]
	at $doGetBean$0(:323) ~[spring-beans-5.2.:5.2.]
	at (:222) ~[spring-beans-5.2.:5.2.]
	at (:321) ~[spring-beans-5.2.:5.2.]
	at (:202) ~[spring-beans-5.2.:5.2.]
	at (:879) ~[spring-beans-5.2.:5.2.]
	at (:878) ~[spring-context-5.2.:5.2.]
	at (:550) ~[spring-context-5.2.:5.2.]
	at (:141) ~[spring-boot-2.2.:2.2.]
	at (:747) [spring-boot-2.2.:2.2.]
	at (:397) [spring-boot-2.2.:2.2.]
	at (:315) [spring-boot-2.2.:2.2.]
	at (:1226) [spring-boot-2.2.:2.2.]
	at (:1215) [spring-boot-2.2.:2.2.]
	at (:19) [classes/:na]
Caused by: : Error creating bean with name 'eduTeacherServiceImpl': Unsatisfied dependency expressed through field 'baseMapper'; nested exception is : Error creating bean with name 'eduTeacherMapper' defined in file [D:\ajavaproject\guli-parent\service\service-edu\target\classes\com\atguigu\eduservice\mapper\]: Unsatisfied dependency expressed through bean property 'sqlSessionFactory'; nested exception is : Error creating bean with name 'sqlSessionFactory' defined in class path resource [com/baomidou/mybatisplus/autoconfigure/]: Bean instantiation via factory method failed; nested exception is : Failed to instantiate []: Factory method 'sqlSessionFactory' threw exception; nested exception is : Error: GlobalConfigUtils setMetaData Fail !  Cause:: Error: Get proxy targetObject exception !  Cause:: Error creating bean with name '' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$]: Bean instantiation via factory method failed; nested exception is : Failed to instantiate []: Factory method 'dataSource' threw exception; nested exception is $DataSourceBeanCreationException: Failed to determine a suitable driver class
	at $(:639) ~[spring-beans-5.2.:5.2.]
	at (:116) ~[spring-beans-5.2.:5.2.]
	at (:397) ~[spring-beans-5.2.:5.2.]
	at (:1429) ~[spring-beans-5.2.:5.2.]
	at (:594) ~[spring-beans-5.2.:5.2.]
	at (:517) ~[spring-beans-5.2.:5.2.]
	at $doGetBean$0(:323) ~[spring-beans-5.2.:5.2.]
	at (:222) ~[spring-beans-5.2.:5.2.]
	at (:321) ~[spring-beans-5.2.:5.2.]
	at (:202) ~[spring-beans-5.2.:5.2.]
	at (:276) ~[spring-beans-5.2.:5.2.]
	at (:1287) ~[spring-beans-5.2.:5.2.]
	at (:1207) ~[spring-beans-5.2.:5.2.]
	at $(:636) ~[spring-beans-5.2.:5.2.]
	... 19 common frames omitted
Caused by: : Error creating bean with name 'eduTeacherMapper' defined in file [D:\ajavaproject\guli-parent\service\service-edu\target\classes\com\atguigu\eduservice\mapper\]: Unsatisfied dependency expressed through bean property 'sqlSessionFactory'; nested exception is : Error creating bean with name 'sqlSessionFactory' defined in class path resource [com/baomidou/mybatisplus/autoconfigure/]: Bean instantiation via factory method failed; nested exception is : Failed to instantiate []: Factory method 'sqlSessionFactory' threw exception; nested exception is : Error: GlobalConfigUtils setMetaData Fail !  Cause:: Error: Get proxy targetObject exception !  Cause:: Error creating bean with name '' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$]: Bean instantiation via factory method failed; nested exception is : Failed to instantiate []: Factory method 'dataSource' threw exception; nested exception is $DataSourceBeanCreationException: Failed to determine a suitable driver class
	at (:1533) ~[spring-beans-5.2.:5.2.]
	at (:1413) ~[spring-beans-5.2.:5.2.]
	at (:594) ~[spring-beans-5.2.:5.2.]
	at (:517) ~[spring-beans-5.2.:5.2.]
	at $doGetBean$0(:323) ~[spring-beans-5.2.:5.2.]
	at (:222) ~[spring-beans-5.2.:5.2.]
	at (:321) ~[spring-beans-5.2.:5.2.]
	at (:202) ~[spring-beans-5.2.:5.2.]
	at (:276) ~[spring-beans-5.2.:5.2.]
	at (:1287) ~[spring-beans-5.2.:5.2.]
	at (:1207) ~[spring-beans-5.2.:5.2.]
	at $(:636) ~[spring-beans-5.2.:5.2.]
	... 32 common frames omitted
Caused by: : Error creating bean with name 'sqlSessionFactory' defined in class path resource [com/baomidou/mybatisplus/autoconfigure/]: Bean instantiation via factory method failed; nested exception is : Failed to instantiate []: Factory method 'sqlSessionFactory' threw exception; nested exception is : Error: GlobalConfigUtils setMetaData Fail !  Cause:: Error: Get proxy targetObject exception !  Cause:: Error creating bean with name '' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$]: Bean instantiation via factory method failed; nested exception is : Failed to instantiate []: Factory method 'dataSource' threw exception; nested exception is $DataSourceBeanCreationException: Failed to determine a suitable driver class
	at (:645) ~[spring-beans-5.2.:5.2.]
	at (:625) ~[spring-beans-5.2.:5.2.]
	at (:1338) ~[spring-beans-5.2.:5.2.]
	at (:1177) ~[spring-beans-5.2.:5.2.]
	at (:557) ~[spring-beans-5.2.:5.2.]
	at (:517) ~[spring-beans-5.2.:5.2.]
	at $doGetBean$0(:323) ~[spring-beans-5.2.:5.2.]
	at (:222) ~[spring-beans-5.2.:5.2.]
	at (:321) ~[spring-beans-5.2.:5.2.]
	at (:202) ~[spring-beans-5.2.:5.2.]
	at (:276) ~[spring-beans-5.2.:5.2.]
	at (:1287) ~[spring-beans-5.2.:5.2.]
	at (:1207) ~[spring-beans-5.2.:5.2.]
	at (:1518) ~[spring-beans-5.2.:5.2.]
	... 43 common frames omitted
Caused by: : Failed to instantiate []: Factory method 'sqlSessionFactory' threw exception; nested exception is : Error: GlobalConfigUtils setMetaData Fail !  Cause:: Error: Get proxy targetObject exception !  Cause:: Error creating bean with name '' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$]: Bean instantiation via factory method failed; nested exception is : Failed to instantiate []: Factory method 'dataSource' threw exception; nested exception is $DataSourceBeanCreationException: Failed to determine a suitable driver class
	at (:185) ~[spring-beans-5.2.:5.2.]
	at (:640) ~[spring-beans-5.2.:5.2.]
	... 56 common frames omitted
Caused by: : Error: GlobalConfigUtils setMetaData Fail !  Cause:: Error: Get proxy targetObject exception !  Cause:: Error creating bean with name '' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$]: Bean instantiation via factory method failed; nested exception is : Failed to instantiate []: Factory method 'dataSource' threw exception; nested exception is $DataSourceBeanCreationException: Failed to determine a suitable driver class
	at (:51) ~[mybatis-plus-core-3.0.:na]
	at (:602) ~[mybatis-plus-extension-3.0.:na]
	at (:386) ~[mybatis-plus-extension-3.0.:na]
	at (:683) ~[mybatis-plus-extension-3.0.:na]
	at (:165) ~[mybatis-plus-boot-starter-3.0.:na]
	at $$EnhancerBySpringCGLIB$$$sqlSessionFactory$0(<generated>) ~[mybatis-plus-boot-starter-3.0.:na]
	at $$EnhancerBySpringCGLIB$$c469479b$$FastClassBySpringCGLIB$$(<generated>) ~[mybatis-plus-boot-starter-3.0.:na]
	at (:244) ~[spring-core-5.2.:5.2.]
	at $(:363) ~[spring-context-5.2.:5.2.]
	at $$EnhancerBySpringCGLIB$$(<generated>) ~[mybatis-plus-boot-starter-3.0.:na]
	at .invoke0(Native Method) ~[na:1.8.0_341]
	at (:62) ~[na:1.8.0_341]
	at (:43) ~[na:1.8.0_341]
	at (:498) ~[na:1.8.0_341]
	at (:154) ~[spring-beans-5.2.:5.2.]
	... 57 common frames omitted


Process finished with exit code 1

        首先,看到上面的错误,我以为是数据源的问题,因此我去配置文件里查看相关的配置,仔细对比了DataSource等配置(搜博客有人说他是由于数据库url写错了,因此我认真对比),经过仔细对比,没有问题。又忙活了一阵子,有人提出在springboot启动类上添加注解@EnableAutoConfiguration,添加上后仍然执行报错。

@EnableAutoConfiguration(exclude = {})

        于是又在网上巴拉巴拉一阵搜,得到一个关键信息,发现是编译后target出了问题,执行程序后,在target文件里未生成文件,因此报错数据源有问题。于是在pom文件中配置以下的配置,运行程序,依然执行错误,查看target文件依然没有application配置文件。

<build>
<resources>
    <resource>
        <directory>src/main/java</directory>
        <includes>
            <include>**/*.yml</include>
            <include>**/*.properties</include>
            <include>**/*.xml</include>
        </includes>
        <filtering>false</filtering>
    </resource>
    <resource>
        <directory>src/main/resources</directory>
        <includes>
            <include>**/*.yml</include>
            <include>**/*.properties</include>
            <include>**/*.xml</include>
        </includes>
        <filtering>false</filtering>
    </resource>
</resources>

</build>

        于是,我根据以往的编译文件,将此项目的xml文件以及文件复制到target文件的相应目录下,然后执行程序,结果程序正常运行了,那么知道具体的什么原因了(刚开始是猜的)。

        紧接着搜索是什么原因导致编译后target中没有生成文件,搜索了一大堆,都没有解决,下班了都没解决。

3、解决方式

        早上来了,脑袋一热,想到会不会是打包方式的问题,然后简单一搜发现,最小子项目中的pom文件中打包方式为pom类型,删除pom打包方式,使用mvn clear清除target,然后执行程序,程序执行正常了,此时生成的target中也包含了xml文件和application配置文件。

        于是又去网上搜索学习了maven 的三种打包方式:pom、jar、war,在pom文件中不写的情况下,默认为jar打包方式。

  1. pom是maven依赖文件
  2. jar是java普通项目打包文件
  3. war是javaweb项目打包文件

具体应用

  1. pom打包方式可以作为其他项目的maven依赖,构建maven项目时可用在父级工程或者聚合工程中,用来做jar包的版本控制
  2. jar包通常是开发时需要应用的类,当需要某些功能时就导入相应的jar包,在构建maven时作为子类;
  3. war包用来发布服务,打成war包后部署到服务器访问。

4、总结

        以上就是我遇到的问题,以及整个过程,最后通过更改打包方式解决了以上问题,可能你的问题与我遇到的问题不同,但是,我相信只要你饱含信心,多思考多搜索多询问,那么你一定能解决,加油,码代码的少年!

学习之所以会想睡觉,是因为那是梦开始的地方。
ଘ(੭ˊᵕˋ)੭ (开心) ଘ(੭ˊᵕˋ)੭ (开心)ଘ(੭ˊᵕˋ)੭ (开心)ଘ(੭ˊᵕˋ)੭ (开心)ଘ(੭ˊᵕˋ)੭ (开心)
                                                                                                         ------不写代码不会凸的小刘