Java Spring到Spring引导迁移。嵌入式Spring数据源

Java Spring到Spring引导迁移。嵌入式Spring数据源,java,hibernate,spring-boot,spring-data-jpa,datasource,Java,Hibernate,Spring Boot,Spring Data Jpa,Datasource,目前,它已被用于SpringBoot应用程序的自定义数据源配置,如下面所示 之前 @SpringBootApplication(exclude={DataSourceAutoConfiguration.class, MongoAutoConfiguration.class}) public class SpringBootAppInitializer { public static void main(String[] args) { System.setPropert

目前,它已被用于SpringBoot应用程序的自定义数据源配置,如下面所示

之前

@SpringBootApplication(exclude={DataSourceAutoConfiguration.class, MongoAutoConfiguration.class})
public class SpringBootAppInitializer {

    public static void main(String[] args) {
        System.setProperty("java.awt.headless", "false");
        SpringApplication.run(SpringBootAppInitializer.class);
    }
}

上述代码正在运行

之后 (要使用嵌入式数据源,请删除数据源自动配置排除或vs)

另外,我正试图使用两组相同的属性使其正常工作,一组是整个教程中使用的标准属性,另一组是Spring boot中嵌入的Hikari impl。 Octava项目的业务子模块中的D:\Projects\Octava\Business\src\main\resources\application-businessProduction.properties文件的一部分

spring.datasource.platform=postgresql
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.data=data-postgresql.sql
spring.datasource.url=jdbc:postgresql://localhost:5432/octavadb
spring.datasource.initialization-mode=always
spring.datasource.username=postgres
spring.datasource.password=1

duplicated props for HIkari impl
spring.datasource.hikari.connectionTimeout=30000
spring.datasource.hikari.idleTimeout=600000
spring.datasource.hikari.maxLifetime=1800000
spring.datasource.hikari.driver-class-name=org.postgresql.Driver
spring.datasource.hikari.data-source-properties.hibernate.hbm2ddl.import_files=classpath:data-postgresql.sql
spring.datasource.hikari.jdbc-url=jdbc:postgresql://localhost:5432/octavadb
spring.datasource.hikari.username=postgres
spring.datasource.hikari.password=1
现在,我正在尝试删除自定义数据源创建并使用嵌入式数据源。但这两组属性都不起作用

以下是嵌入式Hikari数据源的初始化

为什么这种方法不起作用,也许还有其他选择

更新:

IntelliJ IDEA重新启动后,将创建数据源,但出现新的异常

com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2020-05-11 21:55:55.599  INFO 12668 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.PostgreSQLDialect
2020-05-11 21:55:55.875 ERROR 12668 --- [           main] o.s.b.web.embedded.tomcat.TomcatStarter  : Error starting Tomcat context. Exception: org.springframework.beans.factory.BeanCreationException. Message: Error creating bean with name 'securityConfig': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userDetailsService': Unsatisfied dependency expressed through method 'setPersistence' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [octava/config/BusinessHibernateConfig.class]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: java/sql/ShardingKey


2020-05-11 21:55:55.906  WARN 12668 --- [           main] o.a.c.loader.WebappClassLoaderBase       : The web application [ROOT] appears to have started a thread named [HikariPool-1 housekeeper] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
 sun.misc.Unsafe.park(Native Method)
 java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:215)
 java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.awaitNanos(AbstractQueuedSynchronizer.java:2078)
 java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:1093)
 java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:809)
 java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1074)
 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134)
 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
 java.lang.Thread.run(Thread.java:748)

所以,我终于把它修好了

  • 主要修复是从这里开始的 这都是关于依赖性的
  • 还有,检查一下这个 在我的例子中没有这样做,但可能会有助于为旧的实现添加关闭/销毁方法
最终属性是(删除了spring.datasource.hikari.prefix的属性)

Gradle改变以使其工作

    compile('com.zaxxer:HikariCP:2.7.6') {
        exclude group: 'org.hibernate', module: 'hibernate-core'
    }
    compile('org.hibernate:hibernate-hikaricp:4.3.8.Final') {
        //they are pulled in separately elsewhere, to avoid version conflicts
        exclude group: 'org.hibernate', module: 'hibernate-core'
        exclude group: 'com.zaxxer', module: 'HikariCP'
    }

我不完全清楚你想做什么。属性显示未嵌入的postgresql数据库的配置。从调试断点看,似乎没有加载属性。如果您实际上正在尝试配置嵌入式数据库(而不是postgres),那么请检查显而易见的情况:您在运行时类路径上有一个嵌入式数据库(H2、HSQL、Derby等),对吗?如果没有得到有用的答案,您可能希望尝试澄清您的问题;我根本不想使用嵌入式数据库。只是尝试将SpringBootOOTB数据源bean配置(DataSource.class)与仅使用属性一起使用。因此,主要目标是不再手动创建DataSourceBean(在我的例子中是BasicDataSource)。UPD:restart my Intelij IDEA数据源开始工作,但使用web应用程序[ROOT]似乎启动了一个名为[Hikariol-1 housekeeper]的线程,但未能停止它。这很可能会造成内存泄漏。
2020-05-11 20:35:23.922 ERROR 20064 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

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

Disconnected from the target VM, address: '127.0.0.1:54586', transport: 'socket'

Process finished with exit code 1
com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2020-05-11 21:55:55.599  INFO 12668 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.PostgreSQLDialect
2020-05-11 21:55:55.875 ERROR 12668 --- [           main] o.s.b.web.embedded.tomcat.TomcatStarter  : Error starting Tomcat context. Exception: org.springframework.beans.factory.BeanCreationException. Message: Error creating bean with name 'securityConfig': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userDetailsService': Unsatisfied dependency expressed through method 'setPersistence' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [octava/config/BusinessHibernateConfig.class]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: java/sql/ShardingKey


2020-05-11 21:55:55.906  WARN 12668 --- [           main] o.a.c.loader.WebappClassLoaderBase       : The web application [ROOT] appears to have started a thread named [HikariPool-1 housekeeper] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
 sun.misc.Unsafe.park(Native Method)
 java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:215)
 java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.awaitNanos(AbstractQueuedSynchronizer.java:2078)
 java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:1093)
 java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:809)
 java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1074)
 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134)
 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
 java.lang.Thread.run(Thread.java:748)
spring.datasource.platform=postgresql
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.data=data-postgresql.sql
spring.datasource.url=jdbc:postgresql://localhost:5432/octavadb
spring.datasource.initialization-mode=always
spring.datasource.username=postgres
spring.datasource.password=1
    compile('com.zaxxer:HikariCP:2.7.6') {
        exclude group: 'org.hibernate', module: 'hibernate-core'
    }
    compile('org.hibernate:hibernate-hikaricp:4.3.8.Final') {
        //they are pulled in separately elsewhere, to avoid version conflicts
        exclude group: 'org.hibernate', module: 'hibernate-core'
        exclude group: 'com.zaxxer', module: 'HikariCP'
    }