0
0

More than 3 years have passed since last update.

【SpringBoot】DataSourceProperties$DataSourceBeanCreationException

Posted at

発生事象

簡単に動作検証のために、SpringBootを使用してWebアプリを作ろうと思ったら、ビルド時に失敗。
下記のようなエラーが出た。

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

データソースの設定に失敗しているらしい?

対応方法

どうやら、後で使おうと思って記載していたMyBatisを記載していたことが原因だったみたいでした。
pom.xmlからMyBatisの定義を削除したら正常に起動しましたが、他にも以下のような解決方法があるみたいです。

1.EnableAutoConfigurationアノテーションをつける。

[プロジェクト名Application.java]ファイルに、アノテーションを追加します。

sample.java
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
@SpringBootApplication
public class MyAppSampleAppApplication {

    public static void main(String[] args) {
        SpringApplication.run(MyAppSampleApplication.class, args);
    }
}

2.素直にデータソースを設定する

application.properties
spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.datasource.driverClassName=org.postgresql.Driver

※ドライバーを予め落としておく必要があります。

補足:複数のデータソースを設定する方法

調べている中で、複数のデータベースを利用したい場合の記述を見つけたので、メモ。
2つのデータソースを指定する方法

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0