Spring Boot嵌入式Apache Derby

huangapple go评论66阅读模式
英文:

Spring Boot Embedded Apache Derby

问题

这是我翻译好的部分:

我知道之前有类似的问题被问过并得到了解答。但是我已经尝试了之前答案中的解决方案,但它们并没有奏效。我已经尽力尝试了很多方法,希望有人能够为我面临的问题提供一些指引。

以下是我的代码:

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");
    }
}

从我的 pom.xml 中:

<dependency>
    <groupId>org.apache.derby</groupId>
    <artifactId>derby</artifactId>
    <version>10.15.2.0</version>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>org.apache.derby</groupId>
    <artifactId>derbyclient</artifactId>
    <version>10.15.2.0</version>
    <scope>runtime</scope>
</dependency>

我正在使用以下命令构建项目:

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 内容执行一些查询。

英文:

I know a Similar question has been asked and answered before. But i've tried the solutions from the previous answers and they are not working out. I've tried out as much as i could and hope someone can shed some lights over the problem i'm facing.

https://stackoverflow.com/questions/44411950/configuring-embedded-derby-in-spring-boot-app

https://stackoverflow.com/questions/43244109/spring-boot-error-using-apache-derby-as-embedded-database

https://stackoverflow.com/questions/32724434/spring-boot-non-embedded-derby-database

And a lot more from the web. But seriously, i don't see a solution yet for the probelm i'm facing.

Here is my code,

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(&quot;com.app&quot;)
@SpringBootApplication
public class ApacheDerbyExample implements CommandLineRunner {
 
    public static void main(String[] args) {
        System.out.println(&quot;STARTING THE APPLICATION&quot;);
        SpringApplication.run(ApacheDerbyExample.class, args);
        System.out.println(&quot;APPLICATION FINISHED&quot;);
    }
  
    @Override
    public void run(String... args) {
        System.out.println(&quot;EXECUTING : command line runner&quot;);
        //org.apache.derby.jdbc.ClientDataSource ds = new org.apache.derby.jdbc.ClientDataSource();
        //ds.setDatabaseName(&quot;mydb&quot;);
    }
}

And from my pom.xml,
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.15.2.0</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derbyclient</artifactId>
<version>10.15.2.0</version>
<scope>runtime</scope>
</dependency>

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

I'm building the project using,

mvn clean package

And when i run,

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

But i'm getting the following error,

Error starting ApplicationContext. To display the conditions report re-run your application with &#39;debug&#39; 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 &#39;dataSource&#39; 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 &#39;dataSource&#39; 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]

I'm just trying to use the Apache Derby Embedded Database in my Application and with the dependencies defined in Maven, I don't know why i'm still getting the error.

I want to be able to use the Embedded Pooled Data Source(Derby) and run some query on CSV content I get from REST APIs.

答案1

得分: 1

我认为,看起来你的类路径中缺少了 JDBC 驱动程序。

英文:

I think ,Looks like you are missing the jdbc driver in your classpath.

答案2

得分: 1

Mani,
请在你的 Application 类中添加以下注解:

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

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

<version>2.7.0</version>
英文:

Mani,
Please add below annotation in your Application class

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

)

Also remove the below version from your pom.xml..

&lt;version&gt;2.7.0&lt;/version&gt;

huangapple
  • 本文由 发表于 2020年5月31日 05:05:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/62108654.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定