春ブーツ:DataSourceを設定するために失敗しました:「URL」属性が指定されず、埋め込まれたデータソースを設定することはできませんでした

Indrajith Ekanayake:

私は私のWebアプリを実行しているときに次のエラーがスローされます。

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class

スローされたエラーの説明を次のように、

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

参照した後、この答えを、私は私が私でいくつかの変更を行うことを知っているpom.xmlファイルを。しかし、私は変更するとStackOverflowの上でも同様のタイプの質問は、私はこのアウトをソートするために助けることができなかったのか分かりません。

私のpom.xml(私は「ウェブ」、「JPA」、「MySQLの」依存関係を追加したプロジェクトを作成する場合)、次のように

    <?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>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.3.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <groupId>com.example</groupId>
        <artifactId>SL2INDUSTRY</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <name>SL2INDUSTRY</name>
        <description>Demo project for Spring Boot</description>

        <properties>
            <java.version>1.8</java.version>
        </properties>

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

            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <scope>runtime</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <!-- https://mvnrepository.com/artifact/org.apache.tomcat.embed/tomcat-embed-jasper -->
            <dependency>
                <groupId>org.apache.tomcat.embed</groupId>
                <artifactId>tomcat-embed-jasper</artifactId>
                <scope>provided</scope>
            </dependency>

            <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-tomcat -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
                <version>2.1.3.RELEASE</version>
            </dependency>

            <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.dataformat/jackson-dataformat-xml -->
            <dependency>
                <groupId>com.fasterxml.jackson.dataformat</groupId>
                <artifactId>jackson-dataformat-xml</artifactId>
                <version>2.9.8</version>
            </dependency>
        </dependencies>

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

    </project>

H2は、私の解決策ではありませんので、私はこのような状況でH2データベースに対処する必要がないことpleseはノート

編集: application.propertiesファイル

spring.mvc.view.suffix=.jsp
サンフランシスコRecio:

あなたがエラーのあなたのトレースを読んでいる場合:

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class

あなたは彼がBDへの接続を設定するには、ひかりデータソースを使用しようとしていることがわかります。どうしたの?

あなたは春ブーツ2を使用し、スプリングブーツのこのバージョンでは、デフォルトの設定を変更しました。

あなたはこのアドレスで見ることができるように。

https://www.baeldung.com/spring-boot-hikari#spring-boot-2

春ブーツ2では、ひかりは、デフォルトのDataSource実装です。

これは、春のブート1.1から変更されているものです。

・ひかりへの依存関係が自動的春ブート・スターター・データ・JPAに含まれています

・自動的今DataSource実装を決定することを発見アルゴリズムは、(リファレンスマニュアルを参照してください)TomcatJDBCの上にひかりを好みます。

私たちは春ブーツ2.xのに基づいてアプリケーションにひかりを使用したい場合はこのように、我々は何の関係もありません

そしてひかりの構成はdiferentです。

その後、私はあなたがたっプールTomcatの使用を望んでいることを想像してみてください。あなたは何をすることができます。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
    <exclusions>
        <exclusion>
            <groupId>com.zaxxer</groupId>
            <artifactId>HikariCP</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.apache.tomcat</groupId>
    <artifactId>tomcat-jdbc</artifactId>
    <version>9.0.10</version>
</dependency>

これは、あなたの春のJPA設定からデフォルトでひかりを除外し、その後、あなたは、Tomcat-JDBCが必要になります。あなたができるのconfigureデ・データソース:

この単純なアプローチは、私たちは@Configurationクラスを作成し、プログラムのDataSource Beanを定義することなく春ブーツTomcatの接続プールを使用して取得することができます。

また、この場合には、我々はH2インメモリ・データベースを使用していることは注目に値します。春ブーツは、データベースのURL、ユーザー、およびパスワードを指定しなくても、私たちのためにH2を自動設定します。

私達はちょうど「のpom.xml」ファイルに対応する依存関係を含める必要があり、春ブーツは、私たちのために、残りを行います。

また、それは「spring.datasource.type」プロパティを使用して、「application.properties」ファイルにデータソースを接続プールを指定春ブーツが使用することを明示的アルゴリズムをスキャンし、接続プールをスキップすることが可能です。

spring.datasource.type=org.apache.tomcat.jdbc.pool.DataSource
// other spring datasource properties
spring.datasource.url=...
spring.datasource.username=...
spring.datasource.password=...
...

ここでは、完全に無効にひかりを参照する、とのconfigure Tomcatを持っています:

https://www.baeldung.com/spring-boot-tomcat-connection-pool

あなたはひかりを使用する場合の構成は、diferentです。

https://www.baeldung.com/spring-boot-hikari

しかし、これら二つのオプションの一つは、あなたの問題を解決します。

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=118560&siteId=1