【问题标题】:NoClassDeffFoundError: org/springframework/data/jpa/repository/RepositoryNoClassDeffFoundError: org/springframework/data/jpa/repository/Repository
【发布时间】:2021-08-17 17:01:15
【问题描述】:

我正在使用 JpaRepository,这是我的代码

public Interface EmpRepository extends JpaRepository<Employee, Integer> {}

class EmployeeServicImpl {

  private EmpRepository empRepository;

  @Autowired
  EmployeeServicImpl (EmpRepository  theRepository) {
    this.empRepository = theRepository;
  }
}

在我的pom.xml 中添加了以下依赖项

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>

在启动应用程序时,从类加载器解析 bean 声明的构造函数:NoClassDeffFoundError : /org/springframework/data/jpa/repository/Repository

【问题讨论】:

标签: spring-boot spring-data-jpa


【解决方案1】:

几点,

  1. 将存储库界面保存在单独的包中。
  2. 在您的存储库中使用 @Repository
  3. EmployeeServicImpl 类中使用@Service@Component 注释。

当您启动应用程序时,Spring 正在尝试查找 EmpRepository 依赖项来实例化您的服务,但由于您未将其声明为 @Repository,因此无法找到存储库。

p>

此外,使用@Service 的原因是EmployeeServicImpl 也可用于Spring。

【讨论】:

  • 请停止建议人们在 Spring Data JPA 接口上使用 @Repository。那是完全没用的。这些也无济于事,因为他的问题在于他的 pom.xml 中的依赖项,而不是 Spring 设置。
  • 哦!我之前不知道的东西。感谢您指出这一点,@M.Deinum。我不会删除答案以使更多傻瓜意识到这一点。 Reason
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-08-31
  • 1970-01-01
  • 1970-01-01
  • 2021-12-22
  • 2020-10-30
  • 2018-05-22
  • 2022-12-02
相关资源
最近更新 更多