Spring Boot-自动装配DataSource Bean [英] Spring Boot - Autowiring a DataSource Bean

查看:764
本文介绍了Spring Boot-自动装配DataSource Bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基本的Spring Boot应用程序,其注释如下:

I have a basic Spring Boot application annotated like this:

@SpringBootApplication
public class ApiApplication {

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

我的application.properties文件中包含以下条目:

I have the following entries in my application.properties file:

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

据我所知,Spring Boot应该能够从这些属性自动自动连接DataSource Bean.

From my understanding Spring Boot should be able to automatically autowire a DataSource Bean from these properties.

但是,如果我尝试:

@Autowired
DataSource dataSource;

在应用程序中的任何地方(@Configuration文件中的f.i.),我在IntelliJ中收到以下错误:

anywhere in my application (f.i. in @Configuration files), I get the following error in IntelliJ:

无法自动装配.找不到'DataSource'类型的bean."

"Could not autowire. No beans of 'DataSource' type found."

有没有明显的东西让我无法使用?

Is there something obvious that I'm missing for this to work?

我只有一个数据源.

推荐答案

bean实际上确实已正确初始化.这可能只是IntelliJ工具提示错误.

The bean actually does get initialized correctly. This is possibly just an IntelliJ tooltip bug.

添加@SuppressWarnings隐藏消息将可以正常工作.

Adding @SuppressWarnings to hide the message will work without further issues.

这篇关于Spring Boot-自动装配DataSource Bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆