Search code examples
javaspringspring-bootspring-data-elasticsearch

Field in required a bean of type that could not be found consider defining a bean of type in your configuration


I'm getting the following error when trying to run my app:

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.

Project structure:

enter image description here

EmployeeRepository:

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

MyApplication:

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

Solution

  • As you have added spring-boot tag I guess you are using sprig data jpa. Your repository interfaces should extend org.springframework.data.repository.Repository (a marker interface) or one of its sub interfaces (usually org.springframework.data.repository.CrudRepository) for instructing spring to provide a runtime implementation of your repository, if any of those interfaces are not extened you'll get

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