我正在尝试将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: {}
Run Code Online (Sandbox Code Playgroud)
我的结构在这里:
.
??? 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
Run Code Online (Sandbox Code Playgroud)
我试图将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
Run Code Online (Sandbox Code Playgroud)
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'
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试自动装配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;
}
}
Run Code Online (Sandbox Code Playgroud)
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);
}
}
Run Code Online (Sandbox Code Playgroud)
我无法找到我失败的地方.
作为@M.Deinum在他的评论中提到它似乎是一个依赖配置问题.您需要依赖spring-jdbc才能自动配置嵌入式数据库.
请确保您已按照 文档进行操作
您还应该查看这个spring-boot-jdb示例
小智 5
Spring boot主要基于这样的原理:将特定的jar放入类路径中将触发相关功能的激活。Spring Boot 在启动时扫描类路径,并将启动“他找到的所有内容”,除非您通过使用注释禁用它。
因此,要让 Spring Boot 初始化数据源,您必须具有以下依赖项之一: - spring-boot-starter-jdbc :将允许使用数据源和 JDBC 内容。- spring-boot-starter-data-jpa :将加载 JPA 和 DataSource 作为子模块
我遇到了类似的错误,我通过添加以下依赖项解决了它
<!-- 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>
Run Code Online (Sandbox Code Playgroud)