Nie można powiązać właściwości w obszarze "spring.datasource" z javax.sql.DataSource

0

Tworzę aplikację w Spring Boot 2.0.0. Domyślnie używam HikariCP. Plik application.yml dla bazy danych PostgreSQL wyglądał następująco

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

Aplikacja z tą konfiguracją dla PostgreSQL działa bez problemu. Po próbie zmiany bazy danych na H2 zmieniłem dane na

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

A bean konfiguracyjny wygląda tak https://pastebin.com/MwTJE8Kp

Podczas kompilacji wyrzuca

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

Dopóki używam bazy danych PostgreSQL, wszystko jest w porządku, ale kiedy zmienię bazę na H2, powoduje to błąd.

0

A dependency do h2 w pomie albo gradle’u dodane?

0

Oczywiście dodane runtime("com.h2database:h2").

1 użytkowników online, w tym zalogowanych: 0, gości: 1