【问题标题】:Java Spring JPA RepositoryJava Spring JPA 存储库
【发布时间】:2016-04-29 17:49:06
【问题描述】:

我是春季菜鸟,我正在为此苦苦挣扎。

基本上,在开始使用 Spring 和 JPA 开发我的服务器之前,我尝试开始一个简单的示例,只是为了习惯这个框架。 我已经成功地使 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 存储库来分享代码 here

任何关于我做错了什么的线索?

【问题讨论】:

  • 你的课程包是什么?只有在与 App 类位于相同或子包中时,才会自动检测您的存储库。
  • @dunni 我删除只是为了简化,但它们是:package com.example.controllers;包 com.example.entities;包 com.example.repository;包 com.example;
  • 假设您拥有所有依赖项。在您的 App 类中,您可以将其放在@SpringBootApplication 下方:@ComponentScan(basePackages="com.example")。然后再试一次。
  • @KennyTaiHuynh SpringBootApplication ComponentScan(basePackages="com.example") public class App { Same exeption :S
  • 我的意思是在@SpringBootApplication 行下方。在另一行中,将 @ComponentScan(basePackages="com.example").

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


【解决方案1】:

这里的第一件事是为什么您需要实现基本接口Repository,因为这样做,您将不会进行通常的 CRUD 操作。对于这样的操作,最好实现CrudRepository。由于您实现了CrudRepository,因此无需定义 findAll() 方法和许多众所周知的其他方法,您可以在提到的文档中找到。

此外,当使用@SpringBootApplication Spring boot 时使用默认值。如果你看到@SpringBootApplication definition 你会看到:

许多 Spring Boot 开发人员的主类总是使用 @Configuration、@EnableAutoConfiguration 和 @ComponentScan 注释。由于这些注解经常一起使用(特别是如果您遵循上述最佳实践),Spring Boot 提供了一个方便的 @SpringBootApplication 替代方案。

@SpringBootApplication 注解等效于使用@Configuration、@EnableAutoConfiguration 和@ComponentScan 及其默认属性:[...]

这意味着当使用 ComponentScan 的默认值时,您的包结构应如下所示:

  1. com.example.model -> 你的实体
  2. com.example.repositoriy -> 你的仓库
  3. com.example.controller -> 控制器
  4. com.example -> MainApplication 类

这是default project structure 的示例 主应用程序类应该在更高级别的包中,然后是其他包。除非您必须使用 @ComponentScan 指定包位置。

因为您是该框架的初学者。我建议您始终在官方文档中查看类定义。

更新

这是我的一个 Spring Boot 项目的示例

对于 JPA,另请参阅 spring guide

【讨论】:

  • 我尝试从 CrudRepository 扩展,并重命名包以匹配您引用的包,但仍然无法正常工作并获得相同的例外
  • 包名并不重要。结构是。应用程序主类应该在包中比其他类更高。查看我的编辑
  • 你还在坚持吗?
  • @HassamAbdelilah 不幸的是 XD
  • @HassamAbdelilah 感谢您的帮助。问题是 JPA 的第二个依赖项
【解决方案2】:

只需使用@Repository 注释您的界面。如果这不起作用,请尝试将 @EnableJPARepositories 添加到主类。

【讨论】:

  • 使用 Repositories 和 EnableJpaRepositories 我得到了一个不同的例外。异常无法自动装配字段:私有 com.example.repository.PersonRepository com.example.controllers.PersonController.repo;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名为“personRepository”的 bean 时出错:调用 init 方法失败;嵌套异常是 java.lang.NoClassDefFoundError: org/springframework/data/repository/query/QueryByExampleExecutor
【解决方案3】:

尝试将@Repository 注解添加到您的PersonRepository,这可能是它找不到它的原因。

【讨论】:

    【解决方案4】:

    您需要使用@Respository 注释您的PersonRepository 接口,否则spring 上下文将无法识别它或为其创建实现。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-07
      • 1970-01-01
      • 1970-01-01
      • 2017-05-26
      • 2019-08-02
      • 2018-08-06
      • 2017-03-16
      • 2018-03-30
      相关资源
      最近更新 更多