错误运行Spring Boot(通过字段表示的不满足的依赖关系)[重复]
Posted
技术标签:
【中文标题】错误运行Spring Boot(通过字段表示的不满足的依赖关系)[重复]【英文标题】:Error Run Spring Boot (Unsatisfied dependency expressed through field) [duplicate] 【发布时间】:2020-02-13 20:40:36 【问题描述】:我正在观看一堂课,我所做的一切都完全像一堂课,但编译时出错了。 遵循代码和类
2019-10-16 22:49:39.528 信息 7668 --- [主要] ConditionEvaluationReportLoggingListener:
启动 ApplicationContext 时出错。显示条件报告 在启用“调试”的情况下重新运行您的应用程序。 2019-10-16 22:49:39.544 错误 7668 --- [主] os.boot.SpringApplication : 应用程序运行失败
org.springframework.beans.factory.UnsatisfiedDependencyException: 创建名为“pessoaController”的 bean 时出错:不满意 通过字段“服务”表达的依赖关系;嵌套异常是 org.springframework.beans.factory.UnsatisfiedDependencyException: 创建名为“pessoaServiceImp”的 bean 时出错:不满意 通过字段“存储库”表达的依赖关系;嵌套异常是 org.springframework.beans.factory.BeanCreationException:错误 创建名为“pessoaRepository”的bean:调用init方法 失败的;嵌套异常是 java.lang.IllegalArgumentException: Failed 为方法 public abstract com.mydream.mydream.Pessoa 创建查询 com.mydream.mydream.PessoaRepository.findOne(java.lang.Long)!不 找到类型 Pessoa 的属性 findOne!在 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:598) ~[spring-beans-5.1.10.RELEASE.jar:5.1.10.RELEASE] 在 org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90) ~[spring-beans-5.1.10.RELEASE.jar:5.1.10.RELEASE] 在 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:376) ~[spring-beans-5.1.10.RELEASE.jar:5.1.10.RELEASE] 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1411) ~[spring-beans-5.1.10.RELEASE.jar:5.1.10.RELEASE] 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:592) ~[spring-beans-5.1.10.RELEASE.jar:5.1.10.RELEASE] 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans-5.1.10.RELEASE.jar:5.1.10.RELEASE] 在 org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
Pessoa.java
package com.mydream.mydream;
import javax.persistence.*;
@Entity(name="PESSOA")
public class Pessoa
@Id
@Column
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String nome;
private String apelido;
public Long getId()
return id;
public void setId(final Long id)
this.id = id;
public String getNome()
return nome;
public void setNome(final String nome)
this.nome = nome;
public String getApelido()
return apelido;
public void setApelido(String apelido)
this.apelido = apelido;
PessoaService.java
package com.mydream.mydream;
import java.util.List;
public interface PessoaService
List<Pessoa>listar();
Pessoa listarId(Long id);
Pessoa add(Pessoa p);
Pessoa edit(Pessoa p);
Pessoa delete(Long id);
PessoaServiceImp.java
package com.mydream.mydream;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class PessoaServiceImp implements PessoaService
@Autowired
private PessoaRepository repository;
@Override
public List<Pessoa> listar()
return repository.findAll();
@Override
public Pessoa listarId(Long id)
// TODO Auto-generated method stub
return null;
@Override
public Pessoa add(Pessoa p)
// TODO Auto-generated method stub
return null;
@Override
public Pessoa edit(Pessoa p)
// TODO Auto-generated method stub
return null;
@Override
public Pessoa delete(Long id)
// TODO Auto-generated method stub
return null;
PessoaRepository.java
package com.mydream.mydream;
import com.mydream.mydream.Pessoa;
import java.util.List;
import org.springframework.data.repository.Repository;
public interface PessoaRepository extends Repository<Pessoa, Long>
List<Pessoa> findAll();
Pessoa findOne(Long id);
Pessoa save(Pessoa p);
void delete(Pessoa p);
PessoaController.java
package com.mydream.mydream;
import com.mydream.mydream.Pessoa;
import com.mydream.mydream.PessoaService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@CrossOrigin(origins = "http://localhost:4200/", maxAge = 3600)
@RestController
@RequestMapping(path= "/pessoas")
public class PessoaController
@Autowired
PessoaService service;
@GetMapping
public List<Pessoa>listar()
return service.listar();
我已经做了我在其他帖子中所做的,但我没有解决办法
【问题讨论】:
正如您在link to duplicate(问题上方)中看到的,将方法重命名为findById
,并将返回类型更改为Optional<Pessoa>
。 --- 或者使用旧版本的 Spring Data (
【参考方案1】:
PessoaService 中缺少@Repository 注释
【讨论】:
在 PessoaService 中?出现错误感谢您的帮助!以上是关于错误运行Spring Boot(通过字段表示的不满足的依赖关系)[重复]的主要内容,如果未能解决你的问题,请参考以下文章
Elasticsearch和Java Spring Boot:java.lang.NoSuchMethodError:
使用 spring boot 运行单元测试,“没有可运行的方法”错误
Spring Boot 和 Thymeleaf 在字段错误时返回 400 错误