繁体   English   中英

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

[英]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

还有其他更好的方法吗?

您可以使用弹簧轮廓。

具有两个概要文件名称db2sysbase并使用@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.

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