java - 无法找到所需类型的 Bean 中的字段 请考虑在配置中定义该类型的 Bean

标签 java spring spring-boot spring-data-elasticsearch

我在尝试运行我的应用程序时收到以下错误:

Field edao in com.alon.service.EmployeeServiceImpl required a bean of type 'com.alon.repository.EmployeeRepository' that could not be found.

The injection point has the following annotations:

  • @org.springframework.beans.factory.annotation.Autowired(required=true)

Action:

Consider defining a bean of type 'com.alon.repository.EmployeeRepository' in your configuration.

项目结构:

enter image description here

员工存储库:

package com.alon.repository;

import com.alon.model.Employee;
import org.springframework.stereotype.Repository;
import java.util.List;

@Repository
public interface EmployeeRepository {
    List<Employee> findByDesignation(String designation);
    void saveAll(List<Employee> employees);
    Iterable<Employee> findAll();
}

EmployeeServiceImpl:

package com.alon.service;

import com.alon.model.Employee;
import com.alon.repository.EmployeeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeRepository edao;

    @Override
    public void saveEmployee(List<Employee> employees) {
        edao.saveAll(employees);
    }

    @Override
    public Iterable<Employee> findAllEmployees() {
        return edao.findAll();
    }

    @Override
    public List<Employee> findByDesignation(String designation) {
        return edao.findByDesignation(designation);
    }
}

我的应用程序:

package com.alon;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

最佳答案

由于您添加了 spring-boot 标签,我猜您正在使用 sprig data jpa。您的存储库接口(interface)应该扩展 org.springframework.data.repository.Repository (标记接口(interface))或其子接口(interface)之一(通常 org.springframework.data.repository.CrudRepository )用于指示 spring 提供存储库的运行时实现,如果这些接口(interface)中的任何一个未扩展,您将得到

bean of type 'com.alon.repository.EmployeeRepository' that could not be found.

关于java - 无法找到所需类型的 Bean 中的字段 请考虑在配置中定义该类型的 Bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56833509/

相关文章:

java - 如何从子类中的父类访问数组?

java - 如果java中没有指定修饰符会发生什么?

spring - 如何在spring boot 2 webclient调用中添加resilience4j retry?

java - 当尝试使用 dynamodb 和 graphql 运行我的 Spring Boot 应用程序时,我不断收到此 @bean 错误

java - Apache Commons exec PumpStreamHandler 连续输入

java - 将字符串 [] 写入文件

java - 在 spring 中并行调用不同的方法

java - 在触发应用程序事件时创建 bean 时,如何避免 Spring 中的死锁?

java - 由 : java. lang.IllegalArgumentException : Not a managed type: & Caused by: org. hibernate.AnnotationException 引起:没有为实体指定标识符:

java - 无法在mysql中使用spring data jpa创建表