Spring Boot 2.x版本,启动报错“Error creating bean with name 'dataSource' defined in class path resource”

序号:01

在使用spingBoot 2.x版本的时候,使用alibaba的druid数据库连接池,
com.github.pagehelper的分页插件1.1.2版本,启动项目报错。

错误日志如下:

1
2
3
4
Error creating bean with name 'dataSource' defined in class path resource [com/alibaba/druid/spring/boot/autoconfigure/DruidDataSourceAutoConfigure.class]:
Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException:
Failed to instantiate [javax.sql.DataSource]: Factory method 'dataSource' threw exception; nested exception is java.lang.NoClassDefFoundError:
org/springframework/boot/bind/RelaxedDataBinder

解决方案一:使用jdbc连接

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

解决方案二:boot版本改为1.5.x版本

解决方案三:

第一步:把druid连接池版本更新到最新1.1.13

第二步:分页插件更新到1.2.3

1
2
3
4
5
6
7
8
9
10
11
12
<!-- 分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
<!-- alibaba的druid数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.13</version>
</dependency>

第三步:使用com.mysql.cj.jdbc.Driver(application.yml文件)

1
2
3
4
5
6
7
8
9
datasource:
name: test
url: jdbc:mysql://127.0.0.1:3306/hometeach?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8
username: root
password: 123456

# 使用druid数据源
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver #就是修改了这一行

Spring Boot 2.x版本,启动报错“Error creating bean with name 'dataSource' defined in class path resource”

http://leofitz1024.github.io/2021/04/08/Spring Boot/01/

作者

xchen

发布于

2021-04-08

更新于

2021-11-22

评论