web-dev-qa-db-ja.com

SpringBoot-データソースBeanの自動配線

このように注釈が付けられた基本的なSpringBootアプリケーションがあります。

@SpringBootApplication
public class ApiApplication {

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

application.propertiesファイルに次のエントリがあります。

spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/db
spring.datasource.username=dbuser
spring.datasource.password=dbpassword

私の理解では、SpringBootはこれらのプロパティからデータソースBeanを自動的に自動配線できるはずです。

しかし、私が試してみると:

@Autowired
DataSource dataSource;

アプリケーションのどこか(@Configurationファイル内)で、IntelliJで次のエラーが発生します:

自動配線できませんでした。「DataSource」タイプのBeanが見つかりませんでした。」

これが機能するために私が欠けている明らかな何かがありますか?

単一のデータソースがあります。

7
Bragolgirith

Beanは実際には正しく初期化されます。これはおそらくIntelliJツールチップのバグです。

@SuppressWarningsを追加してメッセージを非表示にすると、問題なく機能します。

3
Bragolgirith

Intelijは、2016.2でも、@ SpringBootApplicationアノテーションをサポートしていないようです。 @SpringBootApplicationアノテーションを削除して、@ Configuration、@ EnableAutoConfiguration、および@ComponentScanアノテーションに置き換えるか、エラーを無視する必要があります。

2
BrianC