【发布时间】: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