Springboot没有[javax.sql.DataSource]类型的限定bean

时间:2015-09-27 16:29:27

标签: java spring-boot

我正在尝试将application.properties用于bean数据源,但似乎spring boot没有找到该文件或类似的东西。

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: {}

这是我的结构:

 .
├── 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

我尝试将application.properties文件从资源移到配置而没有。 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'
    }

我正在尝试自动装配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);
    }

}

我无法找到我失败的地方。

5 个答案:

答案 0 :(得分:6)

在他的评论中提到的@M. Deinum似乎是一个依赖配置问题。 You need a dependency on spring-jdbc for an embedded database to be auto-configured

请确保您已关注documentation

您还应该查看此spring-boot-jdb sample

答案 1 :(得分:1)

Spring启动主要基于原理,而不是在类路径中放置特定的jar将触发相关功能的激活。 Spring启动时会在启动时扫描类路径并启动“他找到的所有内容”,除非您使用注释禁用它。

因此要让Spring Boot初始化DataSource,您必须具有以下依赖项之一: - spring-boot-starter-jdbc:允许使用DataSource和JDBC的东西。 - spring-boot-starter-data-jpa:将加载JPA,将DataSource作为子模块加载

答案 2 :(得分:0)

我遇到了这个问题,并发现DataSource的实现放在Tomcat库中。因此,对于第8个tomcat,您将包含放置在org.apache.tomcat:tomcat-jdbc:jar:8.0.36

中的template <typename T> // numeric T pair<T, bool> FindMaxNonRepeating(vector<T> const& vec) { unordered_map<T, int> elem2freq; for (auto const& elem : vec) { elem2freq[elem] += 1; } T largest_non_repetitive = std::numeric_limits<T>::min(); bool found = false; for (auto const& item : elem2freq) { if (item.first > largest_non_repetitive && item.second == 1) { largest_non_repetitive = item.first; found = true; } } return {largest_non_repetitive, found}; }

答案 3 :(得分:0)

我遇到了同样的问题。在我的例子中,我通过添加mysql java连接器的依赖项来解决它。

答案 4 :(得分:0)

我遇到了类似的错误,我通过以下依赖项解决了

<!-- 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>