我有我的Springboot应用程序的following code structure:
我对bean NoSuchBeanDefinitionException
的{{1}}例外。
异常追踪:
UserDao
UserDao.java接口的源代码是
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.matlb.dao.UserDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotat`enter code here`ion.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1373) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1119) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE]
... 37 common frames omitted
我在这里自动装配
package com.matlb.dao;
import com.matlb.domain.User;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface UserDao extends CrudRepository<User,Integer>{
User findByEmail(String email);
}
主类来源
package com.matlb.service;
import com.matlb.dao.UserDao;
import com.matlb.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public List<User> findAll() {
return (List<User>) getUserDao().findAll();
}
@Override
public User createUser(String email) {
User user = new User(email);
return saveUser(user);
}
@Override
public User findById(Integer userId) {
return getUserDao().findOne(userId);
}
@Override
public User saveUser(User user) {
return getUserDao().save(user);
}
@Override
public User findByEmail(String email) {
return getUserDao().findByEmail(email);
}
@Override
public void delete(Integer userId) {
getUserDao().delete(userId);
}
public UserDao getUserDao() {
return userDao;
}
}
build.gradle
package com.matlb;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@SpringBootApplication
public class MatlbApplication {
public static void main(String[] args) {
SpringApplication.run(MatlbApplication.class, args);
}
}
请让我知道我做错了什么。我使用Intellij作为IDE,并在使用buildscript {
ext {
springBootVersion = '1.3.3.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath("org.springframework:springloaded:1.2.1.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
jar {
baseName = 'demo'
version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-aop')
compile('org.springframework.boot:spring-boot-starter-web')
compile("org.springframework.boot:spring-boot-devtools")
compile("org.springframework.boot:spring-boot-starter-data-jpa")
runtime('mysql:mysql-connector-java')
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile('org.springframework.restdocs:spring-restdocs-mockmvc')
}
idea {
module {
inheritOutputDirs = false
outputDir = file("$buildDir/classes/main/")
}
}
eclipse {
classpath {
containers.remove('org.eclipse.jdt.launching.JRE_CONTAINER')
containers 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8'
}
}
task wrapper(type: Wrapper) {
gradleVersion = '2.9'
}
注释时创建了显示bean。
答案 0 :(得分:9)
首先,您需要从DAO界面中删除@Repository。 Spring Data JPA将构建实现并将其部署在Spring容器中,而不使用@Repository。 @EnableJpaRepository将向String Data Jpa发出指令。 Spring Boot自动配置将为您声明@ EnableJpaRepository`。
然后,用JpaRepository替换CrudRepository。
最后,请确保已将spring-boot-starter-data-jpa
声明为maven依赖项。
此致 丹尼尔
答案 1 :(得分:0)
命名并正确放置包装,
com.app
-SpringBootApplication
com.app.controller
-控制器类
com.app.repo
-Repo类超出了CrudRepository
com.app.model
-您的表类(实体)
答案 2 :(得分:0)
@Daniel已经为Spring Data连接存储库提供了很好的背景知识。
对于调试,由于某种原因,Spring无法初始化从CrudRepository
扩展的存储库时不显示任何错误。要进行调试,请将扩展类从CrudRepository
更改为JpaRepository
。现在,Spring将说明为什么它不能初始化存储库(在我的情况下,这是该实体未正确映射到表)。解决这些错误后,您可以切换回CrudRepository
。它将起作用。它对我有用。