天天看點

spring boot 2.x版本:啟動報錯:Error creating bean with name 'dataSource' defined in class path resource#在使用spingBoot 2.x版本的時候,使用alibaba的druid資料庫連接配接池,com.github.pagehelper的分頁插件1.1.2版本,啟動項目報錯。

#在使用spingBoot 2.x版本的時候,使用alibaba的druid資料庫連接配接池,com.github.pagehelper的分頁插件1.1.2版本,啟動項目報錯。

錯誤日志如下:

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連接配接

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
           

解決方案二:boot版本改為1.5.x版本

解決方案3:

第一步、把druid連接配接池版本更新到最新1.1.13

第二步、分頁插件更新到1.2.3

<!-- 分頁插件 -->
<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檔案)

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 #就是修改了這一行