[英]JPA config with multiple repository implementation for a single repository
我有一種情況,我需要根據數據庫從哪個存儲庫實現中獲取數據,對一個存儲庫進行2種不同的實現(一次只能激活一個存儲庫)。
示例: GroupRepository extends CrudRepository <String,Group>{} GroupSybaseRepository extends GroupRepository <String,Group>{} GroupDB2Repository extends GroupRepository <String,Group>{}
我打算在不同的程序包中使用這些存儲庫實現的解決方案,並在其中使用includeFilters / excludeFilters
還有其他更好的方法嗎?
您可以使用彈簧輪廓。
具有兩個概要文件名稱db2
和sysbase
並使用@Profile
注釋相應地標記您的存儲庫。
在運行應用程序時,請使用spring.profiles.active
系統屬性指定要激活的概要文件。
你可以在這里找到一個例子
您可以在單獨的位置使用兩個存儲庫,但是可以通過多種方式來實現(可能是:-))
但可能是:
使用不同的事務管理器,如下所示:-
@Repository()
@Transactional(value = "syBaseTransactionManager")
public interface GroupSybaseRepository extends GroupRepository <String,Group>{
}
@Repository()
@Transactional(value = "db2TransactionManager")
public interface GroupDB2Repository extends GroupRepository <String,Group>{
}
現在,您顯然將在您的上下文中擁有多個事務管理器。 謝謝。
同樣,您也可以有條件存儲庫。 包和事務管理器Bean以及其他相關Bean的文件可能是不同的配置(標記為@Configuration的類標記為@Conditional基於某些環境變量)文件
我使用了包含/排除過濾器的組合,唯一的壞處是當需要在存儲庫實現之間切換時,我必須注釋/取消注釋特定的過濾器定義。 像下面這樣
<context:component-scan base-package="com.concretepage">
<context:include-filter type="regex" expression="com.concretepage.*.*Util"/>
<context:exclude-filter type="assignable" expression="com.concretepage.service.IUserService"/>
</context:component-scan>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.