【发布时间】: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