Bragolgirith
Bragolgirith

Reputation: 2238

Spring Boot - Autowiring a DataSource Bean

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);
    }
}

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

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

However if I try:

@Autowired
DataSource dataSource;

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

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

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

I have a single DataSource.

Upvotes: 7

Views: 14343

Answers (2)

Bragolgirith
Bragolgirith

Reputation: 2238

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

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

Upvotes: 3

BrianC
BrianC

Reputation: 1822

Intelij apparently even in the 2016.2 still does not support the @SpringBootApplication annotation. You either have to remove the @SpringBootApplication annotation and replace it with the @Configuration, @EnableAutoConfiguration and @ComponentScan annotations or just ignore the errors.

Upvotes: 2

Related Questions