首页 > 解决方案 > 错误构建项目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]

... 在此处输入图像描述 我的班级: 在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

我不明白我做错了什么。

标签: javaspringspring-bootjpa

解决方案


PersonaService缺少@Autowired注释。所以你personaRepository不能通过依赖注入来创建。

你有两个选择:

  1. 您可以使用注释您的personaRepository字段@Autowired
  2. 或者你可以用注释你的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;
   }

   ...
}

推荐阅读