微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – Spring-boot CrudRepository自动装配错误

我有我的 Springboot应用程序的 following code structure

我对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

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

在这里自动装配

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

主类的来源

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

请让我知道我做错了什么.我使用Intellij作为IDE,并在使用@Repository注释时创建它的显示bean.

解决方法

首先,您需要从DAO界面中删除@Repository. Spring Data JPA将构建实现并将其部署在spring容器中,而不使用@Repository. @EnableJpaRepository将向String Data Jpa发出指令. Spring Boot自动配置将为您声明@EnableJpaRepository`.

然后,用JpaRepository替换CrudRepository.

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

问候,丹尼尔

原文地址:https://www.jb51.cc/java/239820.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐