【问题标题】:Spring Error creating bean with name 'covidController' defined in file在文件中定义名称为“covidController”的 Spring 错误创建 bean
【发布时间】:2020-05-17 05:09:49
【问题描述】:

首先,您可能认为/投票这个问题是重复的。让我告诉你,我在 SO 上尝试了几乎所有可能的解决方案,而不是在 SO 上。

我正在为一个项目使用 Spring 框架,该项目基于分层架构。我试图修复启动 Spring 时引发的异常。在过去的几天里,我试图解决这个问题,但我无法解决。 (我是春天的新手)

我有三层:

  • 坚持
  • 休息

当我启动应用程序时,它会抛出一个错误:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'covidController' defined in file [......./layered-architecture-spring/rest/target/classes/com/comp/rest/CovidController.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'covidRepository' defined in com.comp.persistence.CovidRepository defined in @EnableJpaRepositories declared on RestApp: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.List com.comp.persistence.CovidRepository.fetchAllData()! No property fetchAllData found for type Covid!
Caused by: java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.List com.comp.persistence.CovidRepository.fetchAllData()! No property fetchAllData found for type Covid!
Caused by: org.springframework.data.mapping.PropertyReferenceException: No property fetchAllData found for type Covid!
    at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:94) ~[spring-data-commons-2.3.0.RELEASE.jar:2.3.0.RELEASE]

我在 GitHub 上的整个项目:https://github.com/Phoenix404/ssa-layered-assignment

@RestController
public class CovidController {

    private final CovidRepository covidRepository;

    public CovidController(CovidRepository cr) {
        covidRepository = cr;
    }

    @GetMapping("/cdata")
    public List<Covid>  getList() {
        return this.covidRepository.findByLocation("italy");
    }
}
@Repository
public interface CovidRepository extends JpaRepository<Covid, Integer> {

    List<Covid> fetchAllData();

    List<Covid> findByDate(Date date);

    List<Covid> findByLocation(String location);

}

CovidController 是我在 rest 模块中的 Rest 应用控制器。 CovidRepository 在持久化模块中。

我正在使用以下注释来扫描其他 SO 上建议的类,但我仍然收到错误:

@SpringBootApplication
@EnableJpaRepositories("com.comp.**persistence**")
@EntityScan(basePackages = {"com.comp.**"})
@ComponentScan(basePackages = {"com.comp.**"})



@SpringBootApplication
@EnableJpaRepositories("com.comp.persistenc*")
@EntityScan(basePackages = {"com.comp"})
@ComponentScan(basePackages = {"com.comp"})



@SpringBootApplication
@EnableJpaRepositories("com.comp.*")
@EntityScan(basePackages = {"com.comp.*"})
@ComponentScan(basePackages = {"com.comp.*"})

我做错了什么?

【问题讨论】:

  • CovidRepository 中使用findAll 而不是fetchAllData。另外,从存储库中删除该方法。
  • 谢谢,应用启动成功,但我可以看到任何记录http://localhost:8080/cdata/...
  • 快速浏览你的应用这是应用的根本原因Initialization of bean failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'org.springframework.context.annotation.ConfigurationClassPostProcessor.importRegistry' available
  • 通过从存储库中删除方法,应用程序运行成功,但我无法获取数据。

标签: java spring spring-boot architecture spring-data-jpa


【解决方案1】:

感谢 @aniket-sahrawat 解决问题。

当我从covidRepository 中删除方法后,问题就解决了。

【讨论】:

    猜你喜欢
    • 2018-08-11
    • 2018-04-25
    • 2021-04-10
    • 2015-09-24
    • 1970-01-01
    • 2021-01-28
    • 1970-01-01
    • 1970-01-01
    • 2015-03-18
    相关资源
    最近更新 更多