繁体   English   中英

Springboot 没有 [javax.sql.DataSource] 类型的合格 bean

[英]Springboot No qualifying bean of type [javax.sql.DataSource]

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

}

我找不到我失败的地方。

作为@M。 Deinum在他的评论中提到它似乎是一个依赖配置问题。 您需要依赖 spring-jdbc 才能自动配置嵌入式数据库

请确保您已按照文档进行操作

您还应该查看此spring-boot-jdb 示例

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

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

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

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

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM