我在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数据库,一切正常,但当我将基数更改为H2时,会抛出错误。