对于最新稳定版本,请使用Spring Data Elasticsearch 5.5.5spring-doc.cadn.net.cn

创建仓库实例

本节将介绍如何为定义的仓库接口创建实例和 BEAN 定义。spring-doc.cadn.net.cn

Java 配置

使用商店专属的@EnableElasticsearchRepositories在 Java 配置类上做注释,用于定义仓库激活的配置。 关于基于 Java 配置的 Spring 容器介绍,请参见 Spring 参考文档中的 JavaConfigspring-doc.cadn.net.cn

启用 Spring Data 仓库的示例配置类似于以下方式:spring-doc.cadn.net.cn

示例基于注释的仓库配置
@Configuration
@EnableJpaRepositories("com.acme.repositories")
class ApplicationConfiguration {

  @Bean
  EntityManagerFactory entityManagerFactory() {
    // …
  }
}
前面的例子使用了 JPA 专用的注释,你需要根据实际使用的存储模块来更改。同样适用于EntityManagerFactory豆。请参阅关于商店特定配置的相关章节。

属性占位符与Ant风格模式

basePackages属性@EnableElasticsearchRepositories支持${…}属性占位符,这些属性通过环境以及Ant风格的封包模式,如“org.example.**”.spring-doc.cadn.net.cn

以下示例指定了app.scan.packages隐式的属性占位符属性@EnableElasticsearchRepositories.spring-doc.cadn.net.cn

@Configuration
@EnableElasticsearchRepositories("${app.scan.packages}")    (1)
public class ApplicationConfiguration {
  // …
}
1 app.scan.packages属性占位符,将根据环境
@EnableElasticsearchRepositories(["\${app.scan.packages}"]) (1)
class ApplicationConfiguration {
  // …
}
1 app.scan.packages属性占位符,将根据环境

XML 配置

每个春季数据模块都包含一个存储 库该元素允许你定义一个基础包,Spring 会帮你扫描,如下示例所示:spring-doc.cadn.net.cn

通过XML启用Spring数据仓库
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns:beans="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns="http://www.springframework.org/schema/data/jpa"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
    https://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/data/jpa
    https://www.springframework.org/schema/data/jpa/spring-jpa.xsd">

  <jpa:repositories base-package="com.acme.repositories" />

</beans:beans>

在前面的例子中,Spring被指示扫描com.acme.仓库以及所有接口扩展子包存储 库或者其子接口之一。 对于每个找到的接口,基础设施会注册该持久化技术特定的存在工厂豆创建处理查询方法调用的适当代理。 每个豆子注册为由接口名称衍生的豆子名称,因此接口为用户仓库将注册于以下用户仓库. 嵌套仓库接口的 Bean 名称前缀其包含的类型名称。 基础包属性允许万用符,这样你可以定义扫描包的模式。spring-doc.cadn.net.cn

使用Filter

默认情况下,基础设施会接收所有扩展持久化技术的接口存储 库子接口位于配置的基础包下方,并为其创建一个 BEAN 实例。 不过,你可能更细致地控制哪些接口有为 BEAN 实例创建。 为此,可以在仓库声明中使用过滤元素。 语义与 Spring 组件Filter中的元素完全等价。 详情请参见春季参考文档spring-doc.cadn.net.cn

例如,为了排除某些接口作为仓库豆的实例化,你可以使用以下配置:spring-doc.cadn.net.cn

使用Filter
@Configuration
@EnableElasticsearchRepositories(basePackages = "com.acme.repositories",
    includeFilters = { @Filter(type = FilterType.REGEX, pattern = ".*SomeRepository") },
    excludeFilters = { @Filter(type = FilterType.REGEX, pattern = ".*SomeOtherRepository") })
class ApplicationConfiguration {

  @Bean
  EntityManagerFactory entityManagerFactory() {
    // …
  }
}
<repositories base-package="com.acme.repositories">
  <context:include-filter type="regex" expression=".*SomeRepository" />
  <context:exclude-filter type="regex" expression=".*SomeOtherRepository" />
</repositories>

上述示例包含所有以SomeRepository并排除以其他仓库从被实例化的状态中消失。spring-doc.cadn.net.cn

独立用途

你也可以在 Spring 容器之外使用仓库基础设施——例如在 CDI 环境中。你仍然需要在类路径中加入一些 Spring 库,但通常你也可以用程序化设置仓库。提供仓库支持的 Spring Data 模块配备了针对持久化技术的特性RepositoryFactory你可以用的,具体如下:spring-doc.cadn.net.cn

仓库工厂的独立用途
RepositoryFactorySupport factory = … // Instantiate factory here
UserRepository repository = factory.getRepository(UserRepository.class);