【问题标题】:Spring Boot Embedded Apache DerbySpring Boot 嵌入式 Apache Derby
【发布时间】:2020-09-18 08:42:23
【问题描述】:

我知道以前有人问过并回答过类似的问题。但是我已经尝试了以前答案中的解决方案,但它们没有奏效。我已经尽可能多地尝试了,希望有人能对我面临的问题有所了解。

Configuring embedded Derby in Spring Boot app

Spring-boot error using Apache Derby as embedded database

Spring Boot non-embedded Derby database?

还有更多来自网络。但说真的,我还没有看到我面临的问题的解决方案。

这是我的代码,

application.properties

spring.datasource.type=org.apache.commons.dbcp2.BasicDataSource
spring.datasource.url=jdbc:derby:memory:mydb;create=true
spring.datasource.driver-class-name=org.apache.derby.jdbc.EmbeddedDriver

ApacheDerbyExample.java

package com.app;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.boot.CommandLineRunner;

@ComponentScan("com.app")
@SpringBootApplication
public class ApacheDerbyExample implements CommandLineRunner {

    public static void main(String[] args) {
        System.out.println("STARTING THE APPLICATION");
        SpringApplication.run(ApacheDerbyExample.class, args);
        System.out.println("APPLICATION FINISHED");
    }

    @Override
    public void run(String... args) {
        System.out.println("EXECUTING : command line runner");
        //org.apache.derby.jdbc.ClientDataSource ds = new org.apache.derby.jdbc.ClientDataSource();
        //ds.setDatabaseName("mydb");
    }
}

从我的 pom.xml 中, org.apache.derby 德比 10.15.2.0 运行 org.apache.derby 德比客户 10.15.2.0 运行

https://github.com/manikandaraj/practice-code/tree/master/java/spring-boot-apache-derby-embedded

我正在使用构建项目,

mvn clean package

当我跑步时,

java -jar -Dspring.config.location=config/spring-conf/application.properties target/derby-101-0.0.1-SNAPSHOT.jar

但我收到以下错误,

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-05-31 02:15:43.402 ERROR 8733 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Dbcp2.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.commons.dbcp2.BasicDataSource]: Factory method 'dataSource' threw exception; nested exception is java.lang.IllegalStateException: Cannot load driver class: org.apache.derby.jdbc.EmbeddedDriver
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:656) ~[spring-beans-5.2.6.RELEASE.jar!/:5.2.6.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:636) ~[spring-beans-5.2.6.RELEASE.jar!/:5.2.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1338) ~[spring-beans-5.2.6.RELEASE.jar!/:5.2.6.RELEASE]

我只是想在我的应用程序中使用 Apache Derby 嵌入式数据库,并使用 Maven 中定义的依赖项,我不知道为什么我仍然收到错误。

我希望能够使用嵌入式池数据源 (Derby) 并对从 REST API 获得的 CSV 内容运行一些查询。

【问题讨论】:

    标签: java spring-boot derby


    【解决方案1】:

    玛尼, 请在您的应用程序类中添加以下注释

    @EnableAutoConfiguration(exclude = { //
        DataSourceAutoConfiguration.class, //
        DataSourceTransactionManagerAutoConfiguration.class, //
        HibernateJpaAutoConfiguration.class }
    

    )

    同时从你的 pom.xml 中删除以下版本..

    <version>2.7.0</version>
    

    【讨论】:

      【解决方案2】:

      我认为,您的类路径中似乎缺少 jdbc 驱动程序。

      【讨论】:

      • 其实 Apache Derby 和 spring-boot-starter-jdbc 在 Maven 依赖项中,我相信这就足够了。不管怎么说,还是要谢谢你。我尝试将 Derby JAR 添加到 CLASSPATH。它仍然给出了错误。
      猜你喜欢
      • 1970-01-01
      • 2017-08-31
      • 1970-01-01
      • 1970-01-01
      • 2017-11-08
      • 2019-06-25
      • 2016-04-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多