SpringBoot创建空项目失败提示: ‘dataSource’ url error解决办法

后端 潘老师 5个月前 (11-29) 130 ℃ (0) 扫码查看

本文主要讲解关于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&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.panziye.com/back/11979.html
喜欢 (0)
请潘老师喝杯Coffee吧!】
分享 (0)
用户头像
发表我的评论
取消评论
表情 贴图 签到 代码

Hi,您需要填写昵称和邮箱!

  • 昵称【必填】
  • 邮箱【必填】
  • 网址【可选】