代码之家  ›  专栏  ›  技术社区  ›  INDRAJITH EKANAYAKE

Spring Boot:未能配置数据源:未指定“url”属性,无法配置嵌入的数据源

  •  0
  • INDRAJITH EKANAYAKE  · 技术社区  · 5 年前

    运行web应用程序时引发以下错误。

    Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration': Unsatisfied dependency expressed through constructor 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]: 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$DataSourceBeanCreationException: Failed to determine a suitable driver class
    

    抛出错误的描述如下所示,

    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:<br>
            If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
    <br>    If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).
    

    转介后 this answer ,我知道我必须对我的 pom.xml 文件。但我不知道该怎么改,甚至StackOverflow上类似的问题也帮不了我解决这个问题。

    我的 pom.xml文件 如下所示(在创建项目时,我添加了“Web”、“JPA”、“MySQL”依赖项),

        <?xml version="1.0" encoding="UTF-8"?>
        <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.0</modelVersion>
            <parent>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-parent</artifactId>
                <version>2.1.3.RELEASE</version>
                <relativePath/> <!-- lookup parent from repository -->
            </parent>
            <groupId>com.example</groupId>
            <artifactId>SL2INDUSTRY</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <name>SL2INDUSTRY</name>
            <description>Demo project for Spring Boot</description>
    
            <properties>
                <java.version>1.8</java.version>
            </properties>
    
            <dependencies>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-data-jpa</artifactId>
                </dependency>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-web</artifactId>
                </dependency>
    
                <dependency>
                    <groupId>mysql</groupId>
                    <artifactId>mysql-connector-java</artifactId>
                    <scope>runtime</scope>
                </dependency>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-test</artifactId>
                    <scope>test</scope>
                </dependency>
                <!-- https://mvnrepository.com/artifact/org.apache.tomcat.embed/tomcat-embed-jasper -->
                <dependency>
                    <groupId>org.apache.tomcat.embed</groupId>
                    <artifactId>tomcat-embed-jasper</artifactId>
                    <scope>provided</scope>
                </dependency>
    
                <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-tomcat -->
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                    <version>2.1.3.RELEASE</version>
                </dependency>
    
                <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.dataformat/jackson-dataformat-xml -->
                <dependency>
                    <groupId>com.fasterxml.jackson.dataformat</groupId>
                    <artifactId>jackson-dataformat-xml</artifactId>
                    <version>2.9.8</version>
                </dependency>
            </dependencies>
    
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-maven-plugin</artifactId>
                    </plugin>
                </plugins>
            </build>
    
        </project>
    

    请注意,在这种情况下,我不需要处理H2数据库,因此H2不是我的解决方案。

    编辑: application.properties 文件

    spring.mvc.view.suffix=.jsp
    
    0 回复  |  直到 4 年前
        1
  •  4
  •   Francesc Recio    5 年前

    如果您看到错误的痕迹:

    Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration': Unsatisfied dependency expressed through constructor 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]: 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$DataSourceBeanCreationException: Failed to determine a suitable driver class
    

    你会注意到他正试图使用hikari数据源来配置到bd的连接。发生了什么事?

    你用弹簧靴2。到了这个版本的spring boot已经改变了默认配置。

    从这个地址可以看到:

    https://www.baeldung.com/spring-boot-hikari#spring-boot-2

    在Spring Boot 2中,Hikari是默认的数据源实现。

    这是与Spring Boot 1.x不同的地方:

    ·对Hikari的依赖现在自动包含在spring boot starter数据jpa中

    自动确定数据源实现的发现算法现在更喜欢Hikari而不是TomcatJDBC(参见参考手册)。

    因此,如果我们想在基于SpringBoot2.x的应用程序中使用Hikari,我们就没什么可做的了。

    而Hikari的结构是不同的。

    然后,我想你想用tomcat连接池。你可以做:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
        <exclusions>
            <exclusion>
                <groupId>com.zaxxer</groupId>
                <artifactId>HikariCP</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat</groupId>
        <artifactId>tomcat-jdbc</artifactId>
        <version>9.0.10</version>
    </dependency>
    

    默认情况下,这将从spring jpa配置中排除Hikari,然后您将需要tomcat jdbc。您可以配置de datasource:

    这个简单的方法允许我们使用Tomcat连接池获得Spring引导,而不必编写@Configuration类和以编程方式定义数据源bean。

    值得注意的是,在本例中,我们使用的是内存中的H2数据库。Spring Boot将为我们自动配置H2,而不必指定数据库URL、用户和密码。

    我们只需要在pom.xml文件文件和Spring Boot将为我们完成剩下的工作。

    或者,可以跳过Spring Boot使用的连接池扫描算法,并在应用程序.属性文件,使用spring.datasource.type类型属性:

    spring.datasource.type=org.apache.tomcat.jdbc.pool.DataSource
    // other spring datasource properties
    spring.datasource.url=...
    spring.datasource.username=...
    spring.datasource.password=...
    ...
    

    这里有禁用Hikari和配置tomcat的完整参考:

    https://www.baeldung.com/spring-boot-tomcat-connection-pool

    如果您想使用Hikari,配置是不同的:

    https://www.baeldung.com/spring-boot-hikari

    但是这两个选项中的一个可以解决你的问题。

        2
  •  1
  •   Eder Rodrigues    5 年前

    有一些可能性,但第一次有三件事你需要做。

    1-依赖项的特定兼容版本pom.xml文件

    2-需要在上添加数据库驱动程序连接器pom.xml文件

    3-创建应用程序.属性关于src/main/resources/应用程序.属性'目录并将数据库配置放在那里。

    应用程序.属性(注意:使用数据库配置更改de值)

    spring.jpa.hibernate.ddl-auto=create
    
    spring.datasource.url=jdbc:mysql://localhost:3306/db_example
    
    spring.datasource.username=user
    
    spring.datasource.password=ThePassword
    

    您可以在此处检查模板: https://spring.io/guides/gs/accessing-data-mysql/