14

I'm trying to use application.properties to bean datasource but it seems that spring boot does not find the file or something like.

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 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've try to move 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 I'm trying to autowire the 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 cannot find where I'm failing at.

5
  • 1
    Show CompanyApplication Commented Sep 27, 2015 at 19:42
  • 5
    Which Spring Boot version are you using? Do you have spring-boot-starter-jdbc in your dependency list and do you have the MySQL driver present? Commented Sep 28, 2015 at 7:55
  • @M.Deinum was right, I had one dependency left spring-boot-starter-jdbc. Now I'm having problems with com.mysql.jdbc.Driver Cannot load driver class: com.mysql.jdbc.Driver Commented Sep 29, 2015 at 22:12
  • That's it, another dependency left, sorry Commented Sep 29, 2015 at 22:30
  • @M.Deinum can yo reply to stackoverflow.com/questions/35967143/…. Its same think but I am getting where is the issue ? Commented Mar 13, 2016 at 6:30

6 Answers 6

9

As @M. Deinum mentioned in his comment it seems to be a dependency configuration problem. You need a dependency on spring-jdbc for an embedded database to be auto-configured.

Please make sure you've followed on the documentation

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

Sign up to request clarification or add additional context in comments.

1 Comment

Any guidance for the project with nosql? How we can solve this ? Why do we need to put JDBC in pom in case on NOSQL ?
8

I got a similar error and I solved it by adding the following dependency

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
    <version>2.5.0</version>
</dependency>

Comments

6

Spring boot is mostly based on the principle than putting a specific jar in the classpath will trigger the activation of the related functionality. Spring boot is scanning the classpath at startup and will start "everything he found" except if you disable it by using annotation.

So to have Spring Boot initializing a DataSource you must have one of the following dependencies: - spring-boot-starter-jdbc : will allow to use the DataSource and JDBC stuff. - spring-boot-starter-data-jpa : will load the JPA and so the DataSource as a sub-module

Comments

1

This should be the correct declaration of your runner class:

package com.companies;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class CompanyApplication {

    public static void main(String[] args) {
        System.exit(SpringApplication.exit(
            SpringApplication.run(CompanyApplication.class, args)));
    }

}

Amidst other things, it will auto-initialize your DataSource from the application.properties.

EDIT: in your application.properties you should have entries similar to these, which are specific for an Oracle DataSource:

spring.datasource.url=jdbc:oracle:thin:@<hostaddr>:<port>:<instance_name>
spring.datasource.username=<username>
spring.datasource.password=<password>
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver

Comments

0

I've faced this problem and figured out that implementation of DataSource is placed in Tomcat libs. So for 8th tomcat you going to include org.apache.tomcat.jdbc.pool.DataSource class that placed in org.apache.tomcat:tomcat-jdbc:jar:8.0.36

Comments

0

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

3 Comments

You should probably be more specific with how did you add the dependency and/or how you figured what dependency to add.
To be honest, I don't really know how to answer here. I figured what dependency to add because you need the mysql java connector in Spring to connect to mySQL-databases. And he knows how to add dependencies. He has already added several in his project. Everybody has who has come this far...
If it's a common thing, then it's ok. I have no experience with Java, just was reviewing first answers, so appologize if the commen was irrelevant

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.