iis服务器助手广告
返回顶部
首页 > 资讯 > 数据库 >Consider the following: If you want an embedded database (H2, HSQL or Derby), please put it on the
  • 692
分享到

Consider the following: If you want an embedded database (H2, HSQL or Derby), please put it on the

javaspringbootspringmysql数据库 2023-08-18 21:08:37 692人浏览 八月长安
摘要

一、问题 在启动SpringBoot项目中遇到如下问题: Description: Failed to configure a DataSource: ‘url’ attribute is not specified and no em

一、问题

在启动SpringBoot项目中遇到如下问题:

Description:
Failed to configure a DataSource: ‘url’ attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class

Action:
Consider the following:
If you want an embedded database (H2, Hsql or Derby), please put it on the classpath.
If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).

这个问题其实是url和数据源配置错误问题,果断查看配置文件和依赖。

另外,本项目用的是mybatisPlus+Mysql,使用默认连接池hakis

———————————————————————————————

二、原代码展示

1、配置文件

server:  port: 9999spring:  Redis:    host: 127.0.0.1    port: 6379  datasource:    driver-class-name: com.mysql.cj.jdbc.Driver    url: jdbc:mysql://localhost:3306/lkd_user?characterEncoding=utf-8&serverTimezone=UTC    username: root    passWord: root
经过校对,没有问题

2、依赖

<project xmlns="Http://Maven.apache.org/POM/4.0.0"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">    <modelVersion>4.0.0modelVersion>    <packaging>pompackaging>    <parent>        <groupId>org.springframework.bootgroupId>        <artifactId>spring-boot-starter-parentartifactId>        <version>2.1.6.RELEASEversion>    parent>    <groupId>com.zbgroupId>    <artifactId>mysecartifactId>    <version>1.0-SNAPSHOTversion>    <dependencies>        <dependency>            <groupId>org.springframework.bootgroupId>            <artifactId>spring-boot-starter-WEBartifactId>        dependency>        <dependency>            <groupId>org.projectlombokgroupId>            <artifactId>lombokartifactId>            <version>1.18.10version>            <scope>providedscope>        dependency>        <dependency>            <groupId>org.springframework.bootgroupId>            <artifactId>spring-boot-starter-securityartifactId>        dependency>                <dependency>            <groupId>org.springframework.bootgroupId>            <artifactId>spring-boot-starter-data-redisartifactId>        dependency>                <dependency>            <groupId>com.alibabagroupId>            <artifactId>fastJSONartifactId>            <version>1.2.33version>        dependency>                <dependency>            <groupId>io.jsonwebtokengroupId>            <artifactId>jJwtartifactId>            <version>0.9.0version>        dependency>        <dependency>            <groupId>com.baomidougroupId>            <artifactId>mybatis-plus-boot-starterartifactId>            <version>3.4.3version>        dependency>        <dependency>            <groupId>mysqlgroupId>            <artifactId>mysql-connector-javaartifactId>        dependency>    dependencies>project>
经过校对,暂时没有发现错误

———————————————————————————————

三、发现

发现没有存在明显的配置文件和依赖的问题,于是又看了下控制台打印日志,真正错误其实在上面,日志级别为warn。

2022-08-21 19:10:31.248 WARN 39656 — [ main] ConfigServletWebServerApplicationContext :
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘userDetailServiceImpl’: Unsatisfied dependency expressed through field ‘tbUserMapper’; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘tbUserMapper’ defined in file [D:\maven\spring\SpringCloud\mysec\target\classes\com\zb\mapper\TbUserMapper.class]: Unsatisfied dependency expressed through bean property ‘sqlSessionFactory’; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘sqlSessionFactory’ defined in class path resource [com/baomidou/mybatisplus/autoconfigure/MybatisPlusAutoConfiguration.class]: Unsatisfied dependency expressed through method ‘sqlSessionFactory’ parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘dataSource’ defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration Hikari.class]:Beaninstantiationviafactorymethodfailed;nestedexceptionisorg.springframework.beans.BeanInstantiationException:Failedtoinstantiate[com.zaxxer.hikari.HikariDataSource]:Factorymetho d ′ dataSourc e ′ threwexception;nestedexceptionisorg.springframework.boot.autoconfigure.jdbc.DataSourceProperties Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties Hikari.class]:Beaninstantiationviafactorymethodfailed;nestedexceptionisorg.springframework.beans.BeanInstantiationException:Failedtoinstantiate[com.zaxxer.hikari.HikariDataSource]:FactorymethoddataSourcethrewexception;nestedexceptionisorg.springframework.boot.autoconfigure.jdbc.DataSourcePropertiesDataSourceBeanCreationException: Failed to determine a suitable driver class

这里打印了bean的依赖问题,如下:

userDetailServiceImpl——依赖——》tbUserMapperl——依赖——》 sqlSessionFactory——依赖——》MybatisPlusAutoConfiguration ——依赖——》dataSource——依赖——》jdbc.DataSourceProperties

这里最终原因是:Failed to determine a suitable driver class,翻译下就是:无法确定合适的驱动程序类。错误主要跟DataSource有关。

———————————————————————————————

四、找原因

1、查看DataSourceConfiguration类,看看导入的实际DataSource配置。

abstract class DataSourceConfiguration {//创建DataBase@SuppressWarnings("unchecked")protected static <T> T createDataSource(DataSourceProperties properties, Class<? extends DataSource> type) {return (T) properties.initializeDataSourceBuilder().type(type).build();}@Configuration@ConditionalOnClass(HikariDataSource.class)@ConditionalOnMissingBean(DataSource.class)@ConditionalOnProperty(name = "spring.datasource.type", havingValue = "com.zaxxer.hikari.HikariDataSource",matchIfMissing = true)static class Hikari {@Bean@ConfigurationProperties(prefix = "spring.datasource.hikari")public HikariDataSource dataSource(DataSourceProperties properties) {HikariDataSource dataSource = createDataSource(properties, HikariDataSource.class);if (StringUtils.hasText(properties.getName())) {dataSource.setPoolName(properties.getName());}return dataSource;}//省略其他Database配置(Tomcat 、dbcp2、generic)}}

这里断点,走的是HikariDataSource的dataSource()工厂方法,然后调用createDataSource()。

评估表达式(ALT+F8)计算createDataSource(),正好是导致异常的最终原因:

org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class

(断点的时候已经发现jdbc爆红,没有成功导入)

继续往下走,进入到DataSourceProperties类initializeDataSourceBuilder()

public DataSourceBuilder<?> initializeDataSourceBuilder() {return DataSourceBuilder.create(getClassLoader()).type(getType()).driverClassName(determineDriverClassName()).url(determineUrl()).username(determineUsername()).password(determinePassword());}

这里采用建造者模式链式调用,方法依次为创建——》类型——》驱动类名——》URL——》用户名——》密码。

评估表达式(ALT+F8)递进式计算:链式调用的返回结果。发现链式调用到driverClassName(determineDriverClassName())报错,并且后面也的determine()全部报错。

(这里已经发现问题,猜测配置文件声明的DriverClassName、url、username、password没有被识别到)

继续往下走,进入到determineDriverClassName()方法

public String determineDriverClassName() {if (StringUtils.hasText(this.driverClassName)) {Assert.state(driverClassIsLoadable(), () -> "Cannot load driver class: " + this.driverClassName);return this.driverClassName;}String driverClassName = null;if (StringUtils.hasText(this.url)) {driverClassName = DatabaseDriver.fromJdbcUrl(this.url).getDriverClassName();}if (!StringUtils.hasText(driverClassName)) {driverClassName = this.embeddedDatabaseConnection.getDriverClassName();}if (!StringUtils.hasText(driverClassName)) {//这里正好是控制台日志warn信息的最后一行提示throw new DataSourceBeanCreationException("Failed to determine a suitable driver class", this,this.embeddedDatabaseConnection);}return driverClassName;}

运行到这已经结束,错误信息中嵌套的最后异常就是出自这里

DataSourceBeanCreationException: Failed to determine a suitable driver class

断点中发现,该方法的类DataSourceProperties中的字段driverClassName、url、username、password全部为NULL。

于是又打开了其他运行正常的项目,断点上述各个位置,重新debug

首先进入到DataSourceConfiguration类的dataSource方法

在这里插入图片描述

可以看到,这个时候已经完成配置项的读取,往下走到DataSourceProperties类的initializeDataSourceBuilder()方法

在这里插入图片描述

这里并没有引发异常,同时发现determineXXX()方法都能得到对应的值。

到这,可以确定是配置信息没有成功被读取到,也就是YAML配置失效。

———————————————————————————————

五、解决方案

1、检查YAML文件位置、是否存在语法错误(四个配置项都没有成功读取,可以排除)

2、查看POM文件是否存在配置错误

最后发现问题是POM打包方式错误,多写了一行

    <modelVersion>4.0.0modelVersion>    <parent>        <groupId>org.springframework.bootgroupId>        <artifactId>spring-boot-starter-parentartifactId>        <version>2.1.6.RELEASEversion>    parent>

———————————————————————————————

六、建议

1、不用看mybatis的mapper接口和映射文件、实体类。

项目启动时没有查数据库,也就没有调用mapper层,自然不会引发这个错误。就算报错上提示信息也很明确,网上很多说要检查这个,感觉没有必要。

2、看yml/properties配置文件中跟url相关的,是否存在错误。

配置写多了,基本不会出现语法或声明错误,而且有代码提示

3、多断点调试,没必要重复性检查代码是否写错,结果半天也找不出来。

来源地址:https://blog.csdn.net/m0_61849361/article/details/126453996

您可能感兴趣的文档:

--结束END--

本文标题: Consider the following: If you want an embedded database (H2, HSQL or Derby), please put it on the

本文链接: https://www.lsjlt.com/news/374763.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作