无法将“spring.datasource”下的属性绑定到 javax.sql.DataSource

问题描述 投票:0回答:1

我在

Spring Boot 2.0.0
中创建了一个应用程序。我默认使用
HikariCP
application.yml
数据库的
PostgreSQL
文件看起来像这样

spring:
  datasource:
    driver-class-name: org.postgresql.Driver
    jdbcUrl: jdbc:postgresql:database
    username: root
    password: root
    type: com.zaxxer.hikari.HikariDataSource
    # Hikari
    poolName: SpringBootHikariCP
    maximumPoolSize: 5
    minimumIdle: 3
    maxLifetime: 2000000
    connectionTimeout: 30000
    idleTimeout: 30000
    pool-prepared-statements: true
    max-open-prepared-statements: 250
    connection-test-query: SELECT 1
eclipse-link:
  database-platform: org.eclipse.persistence.platform.database.PostgreSQLPlatform
  generate-dll: true
  show-sql: true
  weaving: static

PostgreSQL 应用程序没有问题。我尝试把数据库改成H2后,我把数据改成了

spring:
  datasource:
    driver-class-name: org.h2.Driver
    jdbcUrl: jdbc:h2:mem:testdb
    username: sa
    password:
    type: com.zaxxer.hikari.HikariDataSource
    # Hikari
    poolName: SpringBootHikariCP
    maximumPoolSize: 5
    minimumIdle: 3
    maxLifetime: 2000000
    connectionTimeout: 30000
    idleTimeout: 30000
    pool-prepared-statements: true
    max-open-prepared-statements: 250
    connection-test-query: SELECT 1

spring.h2.console:
  enabled: true
  path: /h2

eclipse-link:
  database-platform: org.eclipse.persistence.platform.database.H2Platform
  generate-dll: true
  show-sql: true
  weaving: static

配置bean看起来像这样https://pastebin.com/MwTJE8Kp

他在编译时抛出

2018-04-15 18:38:56.395 DEBUG 3592 --- [ost-startStop-1] com.zaxxer.hikari.HikariConfig           : Driver class org.h2.Driver not found in Thread context class loader TomcatEmbeddedWebappClassLoader
  context: ROOT
  delegate: true
----------> Parent Classloader:
sun.misc.Launcher$AppClassLoader@18b4aac2
, trying classloader sun.misc.Launcher$AppClassLoader@18b4aac2
2018-04-15 18:38:56.406 ERROR 3592 --- [ost-startStop-1] com.zaxxer.hikari.HikariConfig           : Failed to load driver class org.h2.Driver from HikariConfig class classloader sun.misc.Launcher$AppClassLoader@18b4aac2
2018-04-15 18:38:56.412 ERROR 3592 --- [ost-startStop-1] o.s.b.web.embedded.tomcat.TomcatStarter  : Error starting Tomcat context. Exception: org.springframework.beans.factory.BeanCreationException. Message: Error creating bean with name 'servletEndpointRegistrar' defined in class path resource [org/springframework/boot/actuate/autoconfigure/endpoint/web/ServletEndpointManagementContextConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.endpoint.web.ServletEndpointRegistrar]: Factory method 'servletEndpointRegistrar' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'healthEndpoint' defined in class path resource [org/springframework/boot/actuate/autoconfigure/health/HealthEndpointConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.health.HealthEndpoint]: Factory method 'healthEndpoint' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.actuate.autoconfigure.jdbc.DataSourceHealthIndicatorAutoConfiguration': Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.autoconfigure.jdbc.DataSourceHealthIndicatorAutoConfiguration$$EnhancerBySpringCGLIB$$30a19622]: Constructor threw exception; nested exception is org.springframework.boot.context.properties.ConfigurationPropertiesBindException: Error creating bean with name 'getDatasource': Could not bind properties to 'HikariDataSource' : prefix=spring.datasource, ignoreInvalidFields=false, ignoreUnknownFields=true; nested exception is org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'spring.datasource' to javax.sql.DataSource
2018-04-15 18:38:56.465  WARN 3592 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat

只要我使用的是PostgreSQL数据库,一切都OK,但是当我将base更改为H2时,它会抛出错误。

java database spring eclipse spring-boot
1个回答
0
投票

@Alain-Michel-Commoue 越来越近了:HikariCP.jar 是类路径中缺少的依赖项。您可以通过以下方式轻松获得它:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
        <version>2.3.12.RELEASE</version>
    </dependency>
© www.soinside.com 2019 - 2024. All rights reserved.