Java 弹簧靴积垢存储自动接线错误

Java 弹簧靴积垢存储自动接线错误,java,spring-boot,Java,Spring Boot,我拥有Springboot应用程序的以下功能: 对于beanUserDao,我得到了NoSuchBeanDefinitionException的异常 异常跟踪: Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.matlb.dao.UserDao] found for dependency: expected at least 1

我拥有Springboot应用程序的以下功能:

对于bean
UserDao
,我得到了
NoSuchBeanDefinitionException
的异常

异常跟踪:

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
java接口的源代码是

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);
}
格雷德尔先生

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'
}

请告诉我哪里做错了。我使用Intellij作为IDE,并将其显示为在使用
@Repository
注释时创建的bean。

首先,您需要从DAO接口中删除@Repository。SpringDataJPA将构建实现并将其部署在Spring容器中,而不使用@Repository。正是@EnableJpaRepository将为字符串数据Jpa提供指令。Spring Boot自动配置将为您声明@EnableJpaRepository`

然后,用JpaRepository替换Crudepository

最后,确保您已将
springbootstarter数据jpa
声明为maven依赖项

问候,,
Daniel

正确命名和放置包裹,
com.app
-SpringBootApplication
com.app.controller
-控制器类
com.app.repo
-回购类存货
com.app.model
-您的表类(实体)

@Daniel已经提供了Spring数据如何连接存储库的良好背景


至于调试,由于某些原因,Spring在初始化从
crudepository
扩展的存储库时不会显示任何错误。要进行调试,请将扩展类从
crudepository
更改为
JpaRepository
。现在Spring将说明为什么它不能初始化存储库(在我的例子中,它是未正确映射到表的实体)。修复这些错误后,您可以切换回
crudepository
。它会起作用的。它对我很有用。

您的
用户DAO
只是一个普通的
crudepository
——而不是
JpaRepository
MongoRepository
,或者其他告诉Spring要使用哪个特定后端的类型。这是导致异常的原因吗?这是一个开始。您可能还需要一个
@EnableFooRepositories
注释。我还没有看到在crudepository上使用
@Repository
注释。谢谢@chrylis这是有效的。不知怎的,我无法导入JpaRepository。我正在使用依赖项{compile('org.springframework.boot:springbootstarteraop')compile('org.springframework.boot:springbootstarterweb')compile('org.springframework.boot:springbootstarterweb')compile('org.springframework.boot:springbootstarterdata-jpa'))运行时('mysql:mysql-connector-('org.springframework.boot:springbootstartertest')testCompile('org.springframework.restdocs:springrestdocs mockmvc'))在build.gradle中。似乎你有一个gradle问题。签出你的依赖关系树并确保spring数据jpa是其中的一部分。是的,这个gradle刷新是必需的。感谢@daniel Lavoies为我遇到的Autowire问题提供了完美的答案。我注意到Crudepository在常规情况下不再工作
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);
    }
}
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'
}