【发布时间】:2023-02-22 19:34:35
【问题描述】:
`我正在创建一个如下所示的基本项目......
@Entity
@Table(name="employee")
public class **Employee** {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="id")
private int id;
@Column(name="name")
private String name;
@Column(name="email")
private String email;
@Column(name="bloodgroup")
private String bloodGroup;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getBloodGroup() {
return bloodGroup;
}
public void setBloodGroup(String bloodGroup) {
this.bloodGroup = bloodGroup;
}
}
@RestController
public class **EmployeeController** {
`@Autowired EmployeeService employeeService;`
@GetMapping("/employee/{id}")
public Employee getEmployeeDetails(@PathVariable("id")int id) {
//db call
Employee employee = employeeService.getEmployeeById(id);
return employee;
}
@Service
public class **EmployeeService** {
`@Autowired
private EmployeeRepo employeeRepo;`
public Employee getEmployeeById(int id) {
Employee emp = employeeRepo.findById(id).get();
return emp;
}
}
public interface **EmployeeRepo** extends JpaRepository\<Employee, Integer\>{
}
我创建的上述类是为了运行一个示例 GET 服务。当我运行项目时,出现如下错误。
# Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'employeeController': Unsatisfied dependency expressed through field 'employeeService': Error creating bean with name 'employeeService': Unsatisfied dependency expressed through field 'employeeRepo': No qualifying bean of type 'com.employeeservice.repo.EmployeeRepo' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
# Description:
# Field employeeRepo in com.employeeservice.service.EmployeeService required a bean of type 'com.employeeservice.repo.EmployeeRepo' 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.employeeservice.repo.EmployeeRepo' in your configuration.
请帮我解决这个问题..
谢谢
【问题讨论】:
-
你有一个名为 EmployeeRepo 的类吗?它是否配置为 Spring 组件/服务/...?
-
不,我们只有界面。
-
那是你的问题,因为你的错误信息已经说明了
-
我们不能在接口之上创建@Autowired 吗?
标签: java spring-boot