阅读1分钟
rror 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]:FactorymethoddataSourcethrewexception;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.DataSourceProperties

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

  • 如果需要当前服务需要数据库,就添加数据库配置;
  • 如果当前服务不需要数据库,就用方法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);
            大数据工程/后端工程师/前端划水运动员
          
    粉丝