小编典典

Spring Boot-自动装配DataSource Bean

spring-boot

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

@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

据我了解,Spring Boot应该能够自动从这些属性自动装配DataSource Bean。

但是,如果我尝试:

@Autowired
DataSource dataSource;

在应用程序中的任何位置(@Configuration文件中的fi),我在IntelliJ中收到以下错误:

“无法自动装配。找不到’DataSource’类型的bean。”

有什么明显的我想让它起作用吗?

我只有一个数据源。


阅读 1380

收藏
2020-05-30

共1个答案

小编典典

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

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

2020-05-30