Alon
Alon

Reputation: 11935

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

Upvotes: 0

Views: 2654

Answers (2)

WiPU
WiPU

Reputation: 443

I assume you try to use spring data JPA. What you can check / debug is:

  • Is JpaRepositoriesAutoConfiguration executed? You can see this in the start up log in the debug log level
  • Does something change if you addionally add @EnableJpaRepositories with the corresponding basepackages.
  • Add @ComponentScan with the corresponding packages, normally @SpringBootApplication should do it, but just in case.

you can also check the autconfig documentation: https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-auto-configuration.html

EDIT: see comment from @ali4j: I did not see that it is the generic spring Repository interface and not the spring data interface

regards,WiPu

Upvotes: 0

ali4j
ali4j

Reputation: 522

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.

Upvotes: 2

Related Questions