首页 > 解决方案 > 在文件中定义名称为“covidController”的 Spring 错误创建 bean

问题描述

首先,您可能认为/投票这个问题是重复的。让我告诉你,我已经尝试了几乎所有可能的解决方案,而不是 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是我在休息模块内的休息应用程序控制器。在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.*"})

我究竟做错了什么?

标签: javaspringspring-bootarchitecturespring-data-jpa

解决方案


感谢@aniket-sahrawat 解决了这个问题。

当我从covidRepository.


推荐阅读