我在尝试运行我的应用程序时收到以下错误:
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.
项目结构:
员工存储库:
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)中的任何一个未扩展,您将得到 p>
bean of type 'com.alon.repository.EmployeeRepository' that could not be found.
关于java - 无法找到所需类型的 Bean 中的字段 请考虑在配置中定义该类型的 Bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56833509/