Spring 引导 - 自动装配数据源 Bean
Spring Boot - Autowiring a DataSource Bean
我有一个基本的 Spring 引导应用程序,注释如下:
@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;
在我的应用程序的任何地方(f.i。在@Configuration 文件中),我在 IntelliJ 中收到以下错误:
"Could not autowire. No beans of 'DataSource' type found."
有什么明显的东西让我无法正常工作吗?
我只有一个数据源。
Intelij 显然即使到了2016.2 仍然不支持@SpringBootApplication 注解。您要么必须删除 @SpringBootApplication 注释并将其替换为 @Configuration、@EnableAutoConfiguration 和 @ComponentScan 注释,要么忽略错误。
bean 实际上确实得到了正确的初始化。这可能只是一个 IntelliJ 工具提示错误。
添加@SuppressWarnings 以隐藏消息将不会出现其他问题。
我有一个基本的 Spring 引导应用程序,注释如下:
@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;
在我的应用程序的任何地方(f.i。在@Configuration 文件中),我在 IntelliJ 中收到以下错误:
"Could not autowire. No beans of 'DataSource' type found."
有什么明显的东西让我无法正常工作吗?
我只有一个数据源。
Intelij 显然即使到了2016.2 仍然不支持@SpringBootApplication 注解。您要么必须删除 @SpringBootApplication 注释并将其替换为 @Configuration、@EnableAutoConfiguration 和 @ComponentScan 注释,要么忽略错误。
bean 实际上确实得到了正确的初始化。这可能只是一个 IntelliJ 工具提示错误。
添加@SuppressWarnings 以隐藏消息将不会出现其他问题。