2015-06-04 103 views

回答

2

我們使用這兩種方法,並都做工精細至今:

  • 實現自定義控制器來使用您的自定義服務層
  • 實現自定義庫工廠(例如,從RepositoryFactoryBeanSupport延伸),建立自己的PersistentEntityInformation併爲您的自定義數據存儲類型手動處理CRUD操作。

UPDATE:看這個章的文件:Adding custom behavior to all repositories。這個想法是用你自己的@EnableJpaRepositories(repositoryBaseClass = MyRepositoryImpl.class)替換默認的存儲特定實現。

如果你想建立一個自定義存儲SPI這是一個不同的故事。您可以使用spring-data-keyvalue並實施您自己的KeyValueOperations bean,您爲@EnableMapRepositories指定的bean。作爲該實現的示例,查看spring-data-redis源。這是最簡單的解決方案。

從零開始爲自己的存儲庫構建一個完整的SPI需要更多的代碼。我們遵循spring-data-elasticsearch的資料。您可能需要實現:

  • 元數據:CustomEntityInformationCustomEntityMappingContextCustomPersistentEntityCustomPersistentProperty
  • 集成:@EnableCustomRepositoriesCustomRepositoriesRegistrarCustomRepositoryConfigurationExtensionCustomRepositoryFactoryCustomRepositoryFactoryBean
  • 執行:CustomRepository(基本接口),CustomRepositoryImpl(默認實現)。

對於spring-data-rest支持還需要一些額外的代碼,例如搜索資源不會自動公開,所以我們手動構建搜索資源。那麼你可能想要在頂部添加查詢支持等。

總結,答案是肯定的,可以通過實現自己的存儲SPI,但並不容易。首先應該尋找其他的解決方案,包括:

+0

你能否解釋或者鏈接到文檔這個好嗎?特別是第二個。 – afaulconbridge

+0

添加說明和相關鏈接。 – aux

+0

作爲一個方面說明,我們在一段時間前實現了這個解決方案,但現在看起來工作太多......))我們用spring-data-rest來實現某種通過REST從另一個微程序請求的「遠程資源」 - 但現在我們來到了更簡單的解決方案 - 使用API​​網關,投影和資源處理器來定製服務之間的鏈接。 – aux

相關問題