简体   繁体   English

具有单个存储库的多个存储库实现的JPA配置

[英]JPA config with multiple repository implementation for a single repository

I have a scenario where i need to have 2 different implementation of a single repository (only one repository will be active at a time) based on Database from which that repository implementation is getting the data. 我有一种情况,我需要根据数据库从哪个存储库实现中获取数据,对一个存储库进行2种不同的实现(一次只能激活一个存储库)。

Example: GroupRepository extends CrudRepository <String,Group>{} GroupSybaseRepository extends GroupRepository <String,Group>{} GroupDB2Repository extends GroupRepository <String,Group>{} 示例: GroupRepository extends CrudRepository <String,Group>{} GroupSybaseRepository extends GroupRepository <String,Group>{} GroupDB2Repository extends GroupRepository <String,Group>{}

The solution that i am planning to have these repository implementation in different packages and using includeFilters/excludeFilters with 我打算在不同的程序包中使用这些存储库实现的解决方案,并在其中使用includeFilters / excludeFilters

Any other better approach? 还有其他更好的方法吗?

You can use spring profiles. 您可以使用弹簧轮廓。

Have two profile names db2 and sysbase and Mark your repository accordingly with @Profile annotation. 具有两个概要文件名称db2sysbase并使用@Profile注释相应地标记您的存储库。

While running the application specify which profile you want to be active using spring.profiles.active system property. 在运行应用程序时,请使用spring.profiles.active系统属性指定要激活的概要文件。

You can find an example here 你可以在这里找到一个例子

You can use two Repositories in the separate location, but you can achieve this by multiple ways( may be :-) ) 您可以在单独的位置使用两个存储库,但是可以通过多种方式来实现(可能是:-))

But One could be:- 但可能是:

Using different Transaction Managers Like Below:- 使用不同的事务管理器,如下所示:-

@Repository()
@Transactional(value = "syBaseTransactionManager")
public interface GroupSybaseRepository extends GroupRepository <String,Group>{

}

@Repository()
@Transactional(value = "db2TransactionManager")
public interface GroupDB2Repository extends GroupRepository <String,Group>{

}

Now here you will be having multiple Transaction Managers in your context obviously. 现在,您显然将在您的上下文中拥有多个事务管理器。 Thanks. 谢谢。

And you can have Conditional repositories also for the same. 同样,您也可以有条件存储库。 May be different configuration( classes marked with @Configuration marked with @Conditional based on some environment variable) files for both packages and transaction Manager Beans and other related beans also 包和事务管理器Bean以及其他相关Bean的文件可能是不同的配置(标记为@Configuration的类标记为@Conditional基于某些环境变量)文件

I used a combination of include/exclude filters the only bad part was that i have to comment/un-comment the specific filter definition when need to switch between repositories implementation. 我使用了包含/排除过滤器的组合,唯一的坏处是当需要在存储库实现之间切换时,我必须注释/取消注释特定的过滤器定义。 Something like below 像下面这样

<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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM