行业资讯 2025年08月6日
0 收藏 0 点赞 654 浏览 3785 个字
摘要 :

文章目录 解决方案 1. 添加数据库配置 2. 排除 <Springboot 初始化数据库连接池> 这个步骤 本文主要讲解关于SpringBoot创建空项目失败提示: ‘dataSource&#……




  • 解决方案
  • 1. 添加数据库配置
  • 2. 排除 <Springboot 初始化数据库连接池> 这个步骤

本文主要讲解关于SpringBoot创建空项目失败提示: ‘dataSource’ url error解决办法相关内容,让我们来一起学习下吧!

error starting ApplicationContext. To display the conditions report re-run your application with \'debug\' enabled.
2023-11-29 20:04:59.088 ERROR 30892 --- [           main] o.s.boot.SpringApplication               : Application run failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name \'entityManagerFactory\' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean]: Factory method \'entityManagerFactory\' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name \'scopedTarget.dataSource\' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$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 java.lang.IllegalStateException: Cannot load driver class: com.mysql.cj.jdbc.Driver

划重点:springboot 的@SpringBootApplication注解会自动去初始化数据库连接池,不配置的话会启动失败,如下提示:

Exception encountered during context initialization – cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘dataSource’ defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfigurationDbcp2.class]:Beaninstantiationviafactorymethodfailed;nestedexceptionisorg.springframework.beans.BeanInstantiationException:Failedtoinstantiate[org.apache.commons.dbcp2.BasicDataSource]:Factorymethod′dataSource′threwexception;nestedexceptionisorg.springframework.boot.autoconfigure.jdbc.DataSourcePropertiesDbcp2.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.commons.dbcp2.BasicDataSource]: Factory method ‘dataSource’ threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourcePropertiesDbcp2.class]:Beaninstantiationviafactorymethodfailed;nestedexceptionisorg.springframework.beans.BeanInstantiationException:Failedtoinstantiate[org.apache.commons.dbcp2.BasicDataSource]:Factorymethod′dataSource′threwexception;nestedexceptionisorg.springframework.boot.autoconfigure.jdbc.DataSourcePropertiesDataSourceBeanCreationException: Failed to determine a suitable driver class
INFO – Unregistering JMX-exposed beans on shutdown

解决方案

下面两种解决方案根据情况选择

  • 如果需要当前服务需要数据库,就添加数据库配置;
  • 如果当前服务不需要数据库,就用方法2 排除数据库连接池初始化。

1. 添加数据库配置

/gateway/src/main/resources/application.yml
添加如下代码:

spring:
    datasource:
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://192.168.88.151:3306/springcloud-alibaba-gateway?serverTimezone=UTC&amp;useUnicode=true&characterEncoding=utf8&useSSL=false
        username: root
        password: root

2. 排除 <Springboot 初始化数据库连接池> 这个步骤

在启动类里,如下添加如下代码:

//启动类:/gateway/src/main/java/org/example/GatewayApplication.java
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})

package org.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;


@EnableDiscoveryClient
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class GatewayApplication {
    public static void main(String[] args) {
        SpringApplication.run(GatewayApplication.class, args);
    }
}

以上就是关于SpringBoot创建空项目失败提示: ‘dataSource’ url error解决办法相关的全部内容,希望对你有帮助。欢迎持续关注潘子夜个人博客(www.panziye.com),学习愉快哦!

微信扫一扫

支付宝扫一扫

版权: 转载请注明出处:https://www.zuozi.net/9931.html

管理员

相关推荐
2025-08-06

文章目录 一、Reader 接口概述 1.1 什么是 Reader 接口? 1.2 Reader 与 InputStream 的区别 1.3 …

988
2025-08-06

文章目录 一、事件溯源 (一)核心概念 (二)Kafka与Golang的优势 (三)完整代码实现 二、命令…

465
2025-08-06

文章目录 一、证明GC期间执行native函数的线程仍在运行 二、native线程操作Java对象的影响及处理方…

348
2025-08-06

文章目录 一、事务基础概念 二、MyBatis事务管理机制 (一)JDBC原生事务管理(JdbcTransaction)…

456
2025-08-06

文章目录 一、SnowFlake算法核心原理 二、SnowFlake算法工作流程详解 三、SnowFlake算法的Java代码…

517
2025-08-06

文章目录 一、本地Jar包的加载操作 二、本地Class的加载方法 三、远程Jar包的加载方式 你知道Groo…

832
发表评论
暂无评论

还没有评论呢,快来抢沙发~

助力内容变现

将您的收入提升到一个新的水平

点击联系客服

在线时间:08:00-23:00

客服QQ

122325244

客服电话

400-888-8888

客服邮箱

122325244@qq.com

扫描二维码

关注微信客服号