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: 没有为依赖找到类型为 [javax.sql.DataSource] 的合格 bean:预计至少有 1 个 bean 有资格作为此依赖项的自动装配候选者.依赖注释:{}
这是我的结构:
.├── build.gradle└── src└── 主要├── java│ └── com│ └── 企业│ ├── CompanyApplication.java│ ├── 配置│ │ └── WebMvcConfig.java│ ├──控制器│ │ └── HelloWorldController.java│ └── 模型│ ├── Article.java│ ├── dao接口│ │ └── ArticleDaoInterface.java│ ├── daoTemplates│ │ └── ArticleDao.java│ └── 映射器│ └── ArticleMapper.java├──资源│ └── application.properties└── 网页应用└── 网络信息└── 页数└── 你好.jsp
我尝试将 application.properties 文件从资源移动到配置,但什么也没有.application.properties:
spring.datasource.driver-class-name=com.mysql.jdbc.Driverspring.datasource.url=jdbc:mysql://127.0.0.1:3306/namespring.datasource.username=rootspring.datasource.password=rootspring.datasource.driver-class-name=com.mysql.jdbc.Driver
build.gradle
buildscript {存储库{//需要的reposMavenCentral()maven { url "http://repo.spring.io/snapshot" }maven { url "http://repo.spring.io/milestone" }}依赖{//spring-boot 插件所需的依赖类路径org.springframework.boot:spring-boot-gradle-plugin:1.2.6.RELEASE"}}应用插件:'java'应用插件:'战争'应用插件:'spring-boot'罐子{baseName = '公司'版本 = '0.2'}战争{baseName = '公司'版本 = '0.1'}源兼容性 = 1.8目标兼容性 = 1.8存储库{MavenCentral()Maven { url "http://repo.spring.io/snapshot" }maven { url "http://repo.spring.io/milestone" }}依赖{编译'org.springframework.boot:spring-boot-starter-web'编译(org.springframework.boot:spring-boot-starter")编译(org.springframework:spring-jdbc")编译('org.springframework.boot:spring-boot-starter-jdbc:1.2.6.RELEASE')testCompile("junit:junit")//JSP需要的依赖编译'org.apache.tomcat.embed:tomcat-embed-jasper'}
以及我尝试自动连接数据源的地方:
package com.companies.model.daoTemplates;导入 com.companys.model.Article;导入 com.companies.model.daoInterface.ArticleDaoInterface;导入 com.companies.model.mappers.ArticleMapper;导入 org.springframework.beans.factory.annotation.Autowired;导入 org.springframework.jdbc.core.JdbcTemplate;导入 org.springframework.stereotype.Repository;导入 javax.sql.DataSource;导入 java.util.List;@Repository公共类 ArticleDao 实现了 ArticleDaoInterface {私有 JdbcTemplate jdbcTemplateObject;private final String DB_NAME = "文章";@覆盖@自动连线公共无效 setDataSource(DataSource ds) {this.jdbcTemplateObject = new JdbcTemplate(ds);}@覆盖公共列表<文章>列表文章(){String SQL = "select * from " + DB_NAME + " where inactive = false ORDER BY name";列表<文章>文章 = jdbcTemplateObject.query(SQL,新的文章映射器());返回文章;}}
CompanyApplication.java
package com.companies;导入 org.springframework.boot.SpringApplication;导入 org.springframework.boot.autoconfigure.EnableAutoConfiguration;导入 org.springframework.context.annotation.ComponentScan;导入 org.springframework.context.annotation.Configuration;@配置@ComponentScan@EnableAutoConfiguration公共类公司应用{公共静态无效主(字符串 [] args){SpringApplication.run(CompanyApplication.class, args);}}
我找不到我失败的地方.
As @M.Deinum 在他的评论中提到它似乎是一个依赖配置问题.你需要依赖spring-jdbc用于自动配置嵌入式数据库.
请确保您已遵循文档>
你还应该看看这个 spring-boot-jdb样品
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.
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
这篇关于Springboot 没有 [javax.sql.DataSource] 类型的合格 bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!