Spring Boot - 自动装配DataSource Bean

vto*_*osh 6 spring autowired spring-boot

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

@SpringBootApplication
public class ApiApplication {

    public static void main(String[] args) {
        SpringApplication.run(ApiApplication.class, args);
    }
}
Run Code Online (Sandbox Code Playgroud)

我的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
Run Code Online (Sandbox Code Playgroud)

根据我的理解,Spring Boot应该能够从这些属性自动自动装配DataSource Bean.

但是,如果我尝试:

@Autowired
DataSource dataSource;
Run Code Online (Sandbox Code Playgroud)

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

"无法自动装配.没有找到'DataSource'类型的豆子."

是否有一些显而易见的东西让我无法工作?

我有一个DataSource.

vto*_*osh 3

该bean实际上确实被正确初始化。这可能只是一个 IntelliJ 工具提示错误。

添加 @SuppressWarnings 来隐藏消息将不会出现进一步的问题。