Spring-boot CrudRepository自动装配错误 [英] Spring-boot CrudRepository autowiring error

查看:168
本文介绍了Spring-boot CrudRepository自动装配错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Springboot应用程序的



我得到的例外是 NoSuchBeanDefinitionException for bean UserDao



异常跟踪:

 引起:org.springframework.beans .factory.NoSuchBeanDefinitionException:找不到[com.matlb.dao.UserDao]类型的限定bean用于依赖:预期至少有1个bean符合此依赖关系的autowire候选者。依赖注释:{/ 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]
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个常见帧

使用的源代码rDao.java接口是

  package com.matlb.dao; 

import com.matlb.domain.User;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
公共接口UserDao扩展CrudRepository< User,Integer> {

用户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
公共类UserServiceImpl实现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);
返回saveUser(用户);
}

@Override
public User findById(Integer userId){
return getUserDao()。findOne(userId);
}

@Override
public用户saveUser(用户用户){
return getUserDao()。save(user);
}

@Override
public用户findByEmail(String email){
return getUserDao()。findByEmail(email);
}

@Override
public void delete(Integer userId){
getUserDao()。delete(userId);
}

public UserDao getUserDao(){
return userDao;
}
}

主类来源

  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
公共类MatlbApplication {

public static void main(String [] args){
SpringApplication.run(MatlbApplication.class,args) );
}
}

build.gradle

  buildscript {
ext {
springBootVersion ='1.3.3.RELEASE'
}
repositories {
mavenCentral()
}
依赖项{
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()
}


依赖项{
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'
}
}

任务包装器(类型:包装器) ){
gradleVersion ='2.9'
}

请告诉我们我做错了。我使用Intellij作为IDE,并且当我使用 @Repository 注释时创建它的显示bean。

解决方案

首先,您需要从DAO接口中删除@Repository。 Spring Data JPA将构建实现并将其部署在没有@Repository的Spring容器中。 @EnableJpaRepository将向String Data Jpa发出指令。 Spring Boot autoconfiguration会为你声明@EnableJpaRepository`。



然后,用JpaRepository替换CrudRepository。



最后,请确保已将 spring-boot-starter-data-jpa 声明为maven依赖项。



此致,
Daniel


I have the following code structure of my Springboot Application:

I am getting exception of NoSuchBeanDefinitionException for bean UserDao .

Exception Trace :

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

The source code of UserDao.java interface is

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);
}

and I am autowiring it here

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

Source of Main class

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);
    }
}

build.gradle

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

Please let me know where I'm doing it wrong . I'm using Intellij as IDE and its showing bean as created when I use the @Repository annotation.

解决方案

First of all, you need to remove @Repository from your DAO interface. Spring Data JPA will build the implementation and deploy it in the Spring container without the @Repository. It is the @EnableJpaRepository that will give the instruction to String Data Jpa. Spring Boot autoconfiguration will declare the @EnableJpaRepository`for you.

Then, replace CrudRepository by JpaRepository.

Finally, make sure you have declared spring-boot-starter-data-jpa as a maven dependency.

Regards, Daniel

这篇关于Spring-boot CrudRepository自动装配错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆