最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - Bean instantiation via factory method failed; Unsatisfied dependency expressed through method 'dataSourceScriptDa

programmeradmin0浏览0评论
SpringBootDerbyApplication
package guru.springframework;

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

@SpringBootApplication
public class SpringBootDerbyApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootDerbyApplication.class, args);
    }
}
application.properties
spring.datasource.url=jdbc:derby:memory:passagensdb;create=true
spring.datasource.driver-class-name=.apache.derby.jdbc.EmbeddedDriver
spring.datasource.username=app
spring.datasource.password=app
spring.datasource.type=.apache.tomcat.jdbc.pool.DataSource
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns=".0.0" xmlns:xsi=";
         xsi:schemaLocation=".0.0 .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>.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>.apache.derby</groupId>
        <artifactId>derby</artifactId>
        <version>10.15.2.0</version>
    </dependency>

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

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

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

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

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

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

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

    <dependency>
        <groupId>.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>.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>


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

</project>

Message error:

.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dataSourceScriptDatabaseInitializer' defined in class path resource [/springframework/boot/autoconfigure/sql/init/DataSourceInitializationConfiguration.class]: Unsatisfied dependency expressed through method 'dataSourceScriptDatabaseInitializer' parameter 0; nested exception is .springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Tomcat.class]: Bean instantiation via factory method failed; nested exception is .springframework.beans.BeanInstantiationException: Failed to instantiate [.apache.tomcat.jdbc.pool.DataSource]: Factory method 'dataSource' threw exception; nested exception is java.lang.IllegalStateException: Cannot load driver class: .apache.derby.jdbc.EmbeddedDriver at .springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:800) ~[spring-beans-5.3.25.jar:5.3.25]
SpringBootDerbyApplication
package guru.springframework;

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

@SpringBootApplication
public class SpringBootDerbyApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootDerbyApplication.class, args);
    }
}
application.properties
spring.datasource.url=jdbc:derby:memory:passagensdb;create=true
spring.datasource.driver-class-name=.apache.derby.jdbc.EmbeddedDriver
spring.datasource.username=app
spring.datasource.password=app
spring.datasource.type=.apache.tomcat.jdbc.pool.DataSource
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache./POM/4.0.0" xmlns:xsi="http://www.w3./2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache./POM/4.0.0 http://maven.apache./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>.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>.apache.derby</groupId>
        <artifactId>derby</artifactId>
        <version>10.15.2.0</version>
    </dependency>

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

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

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

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

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

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

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

    <dependency>
        <groupId>.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>.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>


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

</project>

Message error:

.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dataSourceScriptDatabaseInitializer' defined in class path resource [/springframework/boot/autoconfigure/sql/init/DataSourceInitializationConfiguration.class]: Unsatisfied dependency expressed through method 'dataSourceScriptDatabaseInitializer' parameter 0; nested exception is .springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Tomcat.class]: Bean instantiation via factory method failed; nested exception is .springframework.beans.BeanInstantiationException: Failed to instantiate [.apache.tomcat.jdbc.pool.DataSource]: Factory method 'dataSource' threw exception; nested exception is java.lang.IllegalStateException: Cannot load driver class: .apache.derby.jdbc.EmbeddedDriver at .springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:800) ~[spring-beans-5.3.25.jar:5.3.25]
Share Improve this question asked Nov 19, 2024 at 17:12 JamesBJamesB 5291 gold badge10 silver badges36 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

You are using the wrong Derby class. Since Derby 10.15.2, the correct name of the class is .apache.derby.iapi.jdbc.AutoloadedDriver. In fact, you don't need to specify the property spring.datasource.driver-class-name as it will be found automatically:

Spring Boot can deduce the JDBC driver class for most databases from the URL.

Also, since you're using Java 17, you should be using Derby version 10.16.1.1.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论