Springboot No qualification bean of type [javax.sql.DataSource]

I am trying to use application.properties for a bean datasource, but it seems that spring boot is not finding a file or something like that.

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.sql.DataSource] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {} 

Here is my structure:

  . ├── build.gradle └── src └── main ├── java │  └── com │  └── companies │  ├── CompanyApplication.java │  ├── config │  │  └── WebMvcConfig.java │  ├── controller │  │  └── HelloWorldController.java │  └── model │  ├── Article.java │  ├── daoInterface │  │  └── ArticleDaoInterface.java │  ├── daoTemplates │  │  └── ArticleDao.java │  └── mappers │  └── ArticleMapper.java ├── resources │  └── application.properties └── webapp └── WEB-INF └── pages └── hello.jsp 

I am trying to move the application.properties file from resources to config and nothing. application.properties:

 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://127.0.0.1:3306/name spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.jdbc.Driver 

build.gradle

 buildscript { repositories { //Required repos mavenCentral() maven { url "http://repo.spring.io/snapshot" } maven { url "http://repo.spring.io/milestone" } } dependencies { //Required dependency for spring-boot plugin classpath "org.springframework.boot:spring-boot-gradle-plugin:1.2.6.RELEASE" } } apply plugin: 'java' apply plugin: 'war' apply plugin: 'spring-boot' jar { baseName = 'companies' version = '0.2' } war { baseName = 'companies' version = '0.1' } sourceCompatibility = 1.8 targetCompatibility = 1.8 repositories { mavenCentral() maven { url "http://repo.spring.io/snapshot" } maven { url "http://repo.spring.io/milestone" } } dependencies { compile 'org.springframework.boot:spring-boot-starter-web' compile("org.springframework.boot:spring-boot-starter") compile("org.springframework:spring-jdbc") compile('org.springframework.boot:spring-boot-starter-jdbc:1.2.6.RELEASE') testCompile("junit:junit") //Required dependency for JSP compile 'org.apache.tomcat.embed:tomcat-embed-jasper' } 

And where am I trying autowire dataSource:

 package com.companies.model.daoTemplates; import com.companies.model.Article; import com.companies.model.daoInterface.ArticleDaoInterface; import com.companies.model.mappers.ArticleMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Repository; import javax.sql.DataSource; import java.util.List; @Repository public class ArticleDao implements ArticleDaoInterface { private JdbcTemplate jdbcTemplateObject; private final String DB_NAME = "articles"; @Override @Autowired public void setDataSource(DataSource ds) { this.jdbcTemplateObject = new JdbcTemplate(ds); } @Override public List<Article> listArticle() { String SQL = "select * from " + DB_NAME + " where inactive = false ORDER BY name"; List <Article> article = jdbcTemplateObject.query(SQL, new ArticleMapper()); return article; } } 

CompanyApplication.java

 package com.companies; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @Configuration @ComponentScan @EnableAutoConfiguration public class CompanyApplication { public static void main(String[] args) { SpringApplication.run(CompanyApplication.class, args); } } 

I can not find where I am failing.

+6
source share
4 answers

Like @M. Deinum mentioned in his comment seems to be a dependency configuration problem. You need a spring-jdbc dependency to automatically configure the embedded database .

Please make sure you follow the documentation.

You should also check out this spring-boot-jdb sample

+5
source

I ran into this problem and realized that the DataSource implementation is hosted in Tomcat libs. So, for the 8th tomcat you will include the class org.apache.tomcat.jdbc.pool.DataSource , which is placed in org.apache.tomcat:tomcat-jdbc:jar:8.0.36

0
source

I had the same problem. In my case, I solved this by adding a dependency for the mysql java connector.

0
source

Spring loading is basically based on the principle than setting a specific jar in the classpath will trigger the activation of a related function. Spring boot scans the class path at startup and launches "everything it found", unless you disable it using annotation.

So, for Spring Boot to initialize a DataSource, you must have one of the following dependencies: - spring-boot-starter-jdbc: allows you to use the DataSource and JDBC stuff. - spring-boot-starter-data-jpa: load the JPA, and therefore the DataSource as a submodule

0
source

All Articles