如果使用了多个模块,Spring Data如何知道wicht存储到哪个存储库? [英] How does Spring Data know wicht store to back a repository with if multiple modules are used?

查看:102
本文介绍了如果使用了多个模块,Spring Data如何知道wicht存储到哪个存储库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Spring Data项目中,如果我正在使用多种类型的存储库,即JPA存储库和Mongo存储库,并且如果要扩展CrudRepository,那么Spring Data如何知道为该存储库选择哪个存储库?它可以使用JPA或Mongo.是否基于每个持久实体上添加的注释 @Document @Entity ?

In a Spring Data project if I am using multiple types of repositories i.e JPA repository and Mongo repository and if I am extending CrudRepository then how does Spring Data know which store to choose for that repository? It can use JPA or Mongo. Is it based on the annotation @Document or @Entity added on every persisting entity?

推荐答案

存储为Spring Data存储库接口创建的代理的决定仅取决于您的配置设置.假设您具有以下配置:

The decision which store a proxy created for a Spring Data repository interface is only made due to your configuration setup. Assume you have the following config:

@Configuration
@EnableJpaRepositories("com.acme.foo")
@EnableMongoRepositories("com.acme.foo")
class Config { }

由于包com.acme.foo中的接口都被MongoDB和JPA基础结构检测到,因此在某些时候会崩溃.为了解决这个问题,JavaConfig和XML支持都允许您定义包含和排除过滤器,以便您可以使用命名约定,其他注释等:

This is going to blow up at some point as the interfaces in package com.acme.foo are both detected by the MongoDB and JPA infrastructure. To resolve this, both the JavaConfig and XML support allows you to define include and exclude filters so that you can either use naming conventions, additional annotations or the like:

@Configuration
@EnableJpaRepositories(basePackages = "com.acme.foo", 
                       includeFilters = @Filter(JpaRepo.class))
@EnableMongoRepositories(base Packages = "com.acme.foo", 
                         includeFilters = @Filter(MongoRepo.class))
class Config { }

在这种情况下,两个注释@JpaRepo@MongoRepo(由您创建)将用于通过注释相关的存储库接口来有选择地触发检测.

In this case, the two annotations @JpaRepo and @MongoRepo (to be created by you) would be used to selectively trigger the detection by annotating the relevant repository interfaces with them.

真正的自动检测是不可能的,因为很难仅从存储库接口声明中分辨出您要定位的商店,并且在创建Bean定义的时间点我们甚至都不知道基础设施(EntityManager之类).

A real auto-detection is sort of impossible as it's hard to tell which store you're targeting solely from the repository interface declaration and at the point in time when the bean definitions are created we don't even know about any further infrastructure (an EntityManager or the like) yet.

这篇关于如果使用了多个模块,Spring Data如何知道wicht存储到哪个存储库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆