【问题标题】:Springboot No qualifying bean of type [javax.sql.DataSource]Springboot没有[javax.sql.DataSource]类型的限定bean
【发布时间】:2015-12-24 22:31:56
【问题描述】:

我正在尝试将 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'
    }

我正在尝试自动连接数据源:

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

}

我找不到失败的地方。

【问题讨论】:

  • 显示CompanyApplication
  • 您使用的是哪个 Spring Boot 版本?你的依赖列表中是否有spring-boot-starter-jdbc 并且你有 MySQL 驱动程序吗?
  • @M.Deinum 是对的,我只剩下一个依赖项 spring-boot-starter-jdbc。现在我遇到了 com.mysql.jdbc.Driver Cannot load driver class: com.mysql.jdbc.Driver 的问题
  • 就是这样,还剩下一个依赖,抱歉
  • @M.Deinum 可以回复stackoverflow.com/questions/35967143/…。它的想法相同,但我知道问题出在哪里?

标签: java spring-boot


【解决方案1】:

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

请确保您已关注documentation

你也应该看看这个spring-boot-jdb sample

【讨论】:

  • 对使用 nosql 的项目有什么指导吗?我们如何解决这个问题?为什么我们需要将 JDBC 放在 pom 中以防在 NOSQL 上?
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      【解决方案4】:

      这应该是你的跑步者类的正确声明:

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

      除其他外,它会自动从application.properties 初始化您的DataSource

      编辑:在您的 application.properties 中,您应该有与这些类似的条目,它们是特定于 Oracle 数据源的:

      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
      

      【讨论】:

        【解决方案5】:

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

        【讨论】:

          【解决方案6】:

          我遇到了同样的问题。就我而言,我通过添加 mysql java 连接器的依赖项来解决它。

          【讨论】:

          • 您可能应该更具体地说明您是如何添加依赖项和/或您是如何确定要添加什么依赖项的。
          • 说实话,这里我真的不知道怎么回答。我想出了要添加什么依赖项,因为您需要 Spring 中的 mysql java 连接器来连接到 mySQL 数据库。他知道如何添加依赖项。他已经在他的项目中添加了几个。每个人都有走到这一步的人......
          • 如果是普通的东西,那没关系。我没有使用 Java 的经验,只是在查看第一个答案,如果评论无关紧要,请道歉
          猜你喜欢
          • 2016-06-28
          • 2015-03-27
          • 2022-01-18
          • 1970-01-01
          • 2020-02-05
          • 2019-03-11
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多