java - 错误构建项目spring org.springframework.beans.factory.UnsatisfiedDependencyException
问题描述
我正在学习 spring,并且正在做一个基本项目,该项目咨询表格并通过服务显示行。
在构建项目时,我遇到以下错误:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'personaService' defined in file [D:\Daniel\Proyecto-Catalogo\backend\cyberbuzon\target\classes\com\verasoftec\cyberbuzon\negocio\services\PersonaService.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personaRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract com.verasoftec.cyberbuzon.modelo.Persona com.verasoftec.cyberbuzon.negocio.repository.PersonaRepository.edit(com.verasoftec.cyberbuzon.modelo.Persona)! No property edit found for type Persona!
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:769) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:218) ~[spring-beans-5.1.9.RELEASE.jar:5.1.9.RELEASE]
我不明白我做错了什么。
解决方案
您PersonaService
缺少@Autowired
注释。所以你personaRepository
不能通过依赖注入来创建。
你有两个选择:
- 您可以使用注释您的
personaRepository
字段@Autowired
- 或者你可以用注释你的
PersonaService
构造函数@Autowired
你PersonaResource
和你的personaService
领域也是如此。
我建议注释您的构造函数。有关这方面的更多信息,请参阅这篇文章。
所以最后你的代码应该是这样的:
@Service
@Transactional(readOnly = true)
public class PersonaService {
private final PersonaRespository personaRepository;
@Autowired
public PersonaService(PersonaRepository personaRepository) {
this.personaRepository = personaRepository;
}
...
}
你PersonaResource
应该看起来像这样:
@RestController
@RequestMapping("/personas")
public class PersonaResource {
private final PersonaService personaService;
@Autowired
public PersonaResource(PersonaService personaService) {
this.personaService = personaService;
}
...
}
推荐阅读
- excel - Excel Userform - 检查单元格是否为空
- css - Firefox 宽度问题
- r - 如何修改 ggplot2 stat_density_2d 等高线图密度缩放?
- c - calloc 后的 realloc 是否会清零字节?
- excel - 使用 VBA 中的引用单元格更新数据透视表字段
- java - 应用了逻辑的 Twitter API
- python - 脚本不会为 Excel 电子表格的每一行发送电子邮件
- python - 在 Python 中访问嵌套字典以在 for 循环中进行比较
- bash - 我的 bash 命令中的错误“未知谓词 `--output'”指的是什么?
- javascript - Javascript ES2020 中是否有等效的 optionalChaining?