通过工厂方法实例化Bean失败;通过方法“dataSourceScriptDatabaseInitializer

问题描述 投票:0回答:1
SpringBootDerby应用程序
package guru.springframework;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootDerbyApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootDerbyApplication.class, args);
    }
}
应用程序.属性
spring.datasource.url=jdbc:derby:memory:passagensdb;create=true
spring.datasource.driver-class-name=org.apache.derby.jdbc.EmbeddedDriver
spring.datasource.username=app
spring.datasource.password=app
spring.datasource.type=org.apache.tomcat.jdbc.pool.DataSource
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>guru.springframework</groupId>
    <artifactId>spring-boot-apache-derby</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>spring-boot-apache-derby</name>
    <description>Demo project for Spring Boot and Apache Derby</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.8</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>17</java.version>
    </properties>

    <dependencies>
    <!-- Dependência do Apache Derby -->
    <dependency>
        <groupId>org.apache.derby</groupId>
        <artifactId>derby</artifactId>
        <version>10.15.2.0</version>
    </dependency>

    <!-- Dependências do Spring Boot -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    
   <dependency>
    <groupId>org.apache.derby</groupId>
    <artifactId>derbyclient</artifactId>
    <version>10.15.2.0</version> <!-- ou a versão mais recente -->
</dependency>
    

    <dependency>
    <groupId>org.apache.tomcat</groupId>
    <artifactId>tomcat-jdbc</artifactId>
</dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!-- Hibernate Validator (Jakarta Validation) -->
    <dependency>
        <groupId>org.hibernate.validator</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>6.2.1.Final</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish</groupId>
        <artifactId>jakarta.el</artifactId>
        <version>4.0.0</version>
    </dependency>

    <!-- Dependência para testes com JUnit 5 -->
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>5.8.2</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.8.2</version>
        <scope>test</scope>
    </dependency>

    <!-- Dependência de Spring Boot para testes -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

消息错误:

org.springframework.beans.factory.UnsatisfiedDependencyException:创建在类路径资源[org/springframework/boot/autoconfigure/sql/init/DataSourceInitializationConfiguration.class]中定义的名为“dataSourceScriptDatabaseInitializer”的bean时出错:通过方法“dataSourceScriptDatabaseInitializer”表达的依赖关系不满足参数0;嵌套异常是org.springframework.beans.factory.BeanCreationException:创建类路径资源中定义的名称为“dataSource”的bean时出错[org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Tomcat.class]:通过工厂方法实例化Bean失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 [org.apache.tomcat.jdbc.pool.DataSource]:工厂方法“dataSource”抛出异常;嵌套异常是 java.lang.IllegalStateException:无法加载驱动程序类:org.apache.derby.jdbc.EmbeddedDriver 在 org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:800) ~[spring-beans-5.3.25.jar:5.3.25]
java spring-boot derby spring-jdbc
1个回答
0
投票

您使用了错误的 Derby 类。从 Derby 10.15.2 开始,类的正确名称是

org.apache.derby.iapi.jdbc.AutoloadedDriver
。事实上,您不需要指定属性
spring.datasource.driver-class-name
,因为它会被自动找到:

Spring Boot 可以从 URL 推断出大多数数据库的 JDBC 驱动类。

此外,由于您使用的是 Java 17,因此您 应该使用 Derby 版本 10.16.1.1

© www.soinside.com 2019 - 2024. All rights reserved.