首页 > 解决方案 > Spring Boot应用中的接口继承问题

问题描述

我编写了一个小型 Spring Boot 应用程序,它有两种使用配置文件的不同类型的存储库;JpaRepositoryMongoRepository。我创建了一个名为 ' common' 的包,其中保存了包含常用方法的接口。然后我在我的 jpa 和 mongo 存储库中扩展这些接口。然而,这些通用接口也扩展了自定义存储库接口,其中包含以 mongo 和 jpa 方式具有自定义实现的方法。

这是我的存储库包结构:

+----common
-----------CommonExampleRepository.java
+----------custom
-----------------CommonExampleRepositoryCustom.java
+----------mongo
-----------------ExampleRepositoryCustomImpl.java
+----------sql
-----------------ExampleRepositoryCustomImpl.java
+----mongo
--------ExampleRepositoryMongo.java
+----sql
--------ExampleRepositorySql.java

这是一个通用存储库的样子

    @NoRepositoryBean
    public interface CommonRepository extends BaseRepository<EntityExample>, CommonExampleRepositoryCustom {
    //some methods here
    }

这是我的 jpa 特定存储库(mongo 也是如此):

@Profile("sql")
@Primary
public interface ExampleRepositorySql    extends  CommonRepository, 
                                                  JpaRepository<EntityExample, ID> {
}

我希望这个存储库能够工作,因为CommonRepositoryextends CommonExampleRepositoryCustom。然而,事实并非如此。一旦我运行应用程序,它就会显示此错误:

 org.springframework.data.mapping.PropertyReferenceException: No property xxx found for type EntityExample!

我还通过CommonRepositoryCustom像这样扩展 sql 存储库中的 来临时修复它:

@Profile("sql")
@Primary
public interface ExampleRepositorySql    extends  CommonRepository,
                                                  CommonExampleRepositoryCustom
                                                  JpaRepository<EntityExample, ID> {
}

但这根本感觉不对,因为我也在扩展CommonExampleRepositoryCustomCommonRepository否则它不起作用。

有些东西告诉我这与包结构有关,但我不太确定。我尝试了多种组织它们的方法,但没有任何效果。任何帮助,将不胜感激!

标签: spring-bootinterfacespring-data-jparepositoryspring-data-mongodb

解决方案


推荐阅读