[英]Java Spring JPA Repository
我是Spring菜鸟,正在努力。
基本上,在开始使用Spring和JPA一起开发Server之前,我试图开始一个简单的示例,以适应该框架。 我已经成功使make Spring与某些框架一起使用,例如Log4J,Swagger等。 现在,我正在尝试使用JPA,我可以从中找到一些解决方案。
我看到了一些有关如何使用它进行开发的博客,以及从我选择创建我的Repository Interfece并extend Repository<T, ID>
所有成千上万个选项中的博客。 您可以在下面看到我的代码:
package com.example.model;
@Entity
public class Person {
@Id
public Integer id;
public String name;
public Person(){}
}
package com.example.repository;
public interface PersonRepository extends Repository<Person, Integer> {
Collection<Person> findAll();
}
package com.example.controller;
@RestController
public class PersonController {
@Autowired
private PersonRepository repo;
@RequestMapping(value = "/persons", method = RequestMethod.GET)
public Collection<Person> getAll() {
return repo.findAll();
}
}
package com.example;
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
我也有application.properties文件:
spring.datasource.platform=postgres
spring.datasource.url=jdbc:postgresql://localhost:5432/test_db
spring.datasource.username=test
spring.datasource.password=test
spring.datasource.driver-class-name=org.postgresql.Driver
当服务器运行时,出现以下异常:
: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.example.repository.PersonRepository com.example.controllers.PersonController.repo; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.example.repository.PersonRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations:
: Closing JPA EntityManagerFactory for persistence unit 'default'
: Stopping service Tomcat
: Application startup failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.example.repository.PersonRepository com.example.controllers.PersonController.repo; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.example.repository.PersonRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
我创建了一个Github存储库以在此处共享代码。
关于我在做什么错的任何线索吗?
只需使用@Repository注释您的界面。 如果那不起作用,请尝试将@EnableJPARepositories添加到主类。
这里的第一件事是为什么您需要执行基本接口存储库 ,而不用执行通常的CRUD操作。 对于此类操作,最好实现CrudRepository 。 由于您实现了CrudRepository,因此无需定义findAll()方法以及可以在文档中找到的许多其他知名方法。
此外,当使用@SpringBootApplication
Spring启动使用默认值。 如果看到@SpringBootApplication 定义 ,则会看到:
许多Spring Boot开发人员的主类始终带有@ Configuration,@ EnableAutoConfiguration和@ComponentScan注释。 由于这些批注经常一起使用(特别是如果您遵循上述最佳实践),因此Spring Boot提供了一种方便的@SpringBootApplication替代方案。
@SpringBootApplication注释等效于使用@ Configuration,@ EnableAutoConfiguration和@ComponentScan及其默认属性:[...]
这意味着当使用ComponentScan的默认值时,您的程序包结构应如下所示:
这是默认项目结构的示例。主应用程序类应位于其他应用程序的更高级别的程序包中。 除非必须使用@ComponentScan指定软件包的位置。
正如您是该框架的初学者一样。 我建议您始终在官方文档中查看类定义。
更新 :
这是我的一个春季靴子项目的例子
另请参阅本JPA 春季指南
尝试将@Repository批注添加到PersonRepository中,这可能是找不到它的原因。
您需要使用@Respository注释PersonRepository接口,否则spring上下文将无法识别它或为其创建实现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.