对于最新的稳定版本,请使用 Spring Data JPA 3.3.1Spring中文文档

对于最新的稳定版本,请使用 Spring Data JPA 3.3.1Spring中文文档

本节介绍如何通过以下任一方式配置 Spring Data JPA:Spring中文文档

基于注释的配置

Spring Data JPA 存储库支持可以通过 JavaConfig 和自定义 XML 命名空间激活,如以下示例所示:Spring中文文档

例 1.使用 JavaConfig 的 Spring Data JPA 存储库
@Configuration
@EnableJpaRepositories
@EnableTransactionManagement
class ApplicationConfig {

  @Bean
  public DataSource dataSource() {

    EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
    return builder.setType(EmbeddedDatabaseType.HSQL).build();
  }

  @Bean
  public LocalContainerEntityManagerFactoryBean entityManagerFactory() {

    HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
    vendorAdapter.setGenerateDdl(true);

    LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
    factory.setJpaVendorAdapter(vendorAdapter);
    factory.setPackagesToScan("com.acme.domain");
    factory.setDataSource(dataSource());
    return factory;
  }

  @Bean
  public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {

    JpaTransactionManager txManager = new JpaTransactionManager();
    txManager.setEntityManagerFactory(entityManagerFactory);
    return txManager;
  }
}
您必须创建而不是直接创建,因为前者除了创建 .LocalContainerEntityManagerFactoryBeanEntityManagerFactoryEntityManagerFactory

前面的配置类使用 API 设置嵌入式 HSQL 数据库。然后,Spring Data 设置并使用 Hibernate 作为示例持久性提供程序。此处声明的最后一个基础结构组件是 .最后,该示例使用注释激活 Spring Data JPA 存储库,该注释本质上带有与 XML 命名空间相同的属性。如果未配置基本包,则使用配置类所在的基包。EmbeddedDatabaseBuilderspring-jdbcEntityManagerFactoryJpaTransactionManager@EnableJpaRepositoriesSpring中文文档

您必须创建而不是直接创建,因为前者除了创建 .LocalContainerEntityManagerFactoryBeanEntityManagerFactoryEntityManagerFactory

Spring 命名空间

Spring Data 的 JPA 模块包含一个自定义命名空间,允许定义存储库 Bean。它还包含 JPA 特有的某些功能和元素属性。通常,可以使用该元素设置 JPA 存储库,如以下示例所示:repositoriesSpring中文文档

例 2.使用命名空间设置 JPA 存储库
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:jpa="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>
JavaConfig 和 XML 哪个更好?XML 是很久以前 Spring 的配置方式。在当今快速发展的 Java、记录类型、注释等时代,新项目通常尽可能多地使用纯 Java。虽然没有立即删除 XML 支持的计划,但某些最新功能可能无法通过 XML 获得。

使用该元素,它激活了所有用 注释的 bean 的持久性异常转换,以使 JPA 持久性提供程序抛出的异常转换为 Spring 的层次结构。repositories@RepositoryDataAccessExceptionSpring中文文档

自定义命名空间属性

除了元素的缺省属性之外,JPA 命名空间还提供了其他属性,以便您更详细地控制存储库的设置:repositoriesSpring中文文档

表 1.元素的自定义 JPA 特定属性repositories

entity-manager-factory-refSpring中文文档

显式连接要使用的存储库与元素检测到的存储库。如果应用程序中使用了多个 Bean,则通常使用。如果未配置,Spring Data 会自动查找名称为 的 Bean。EntityManagerFactoryrepositoriesEntityManagerFactoryEntityManagerFactoryentityManagerFactoryApplicationContextSpring中文文档

transaction-manager-refSpring中文文档

显式连接要使用的存储库与元素检测到的存储库。通常只有在配置了多个事务管理器或 Bean 时才需要这样做。默认为在当前 .PlatformTransactionManagerrepositoriesEntityManagerFactoryPlatformTransactionManagerApplicationContextSpring中文文档

Spring Data JPA 要求在未定义显式的情况下存在名为 bean。PlatformTransactionManagertransactionManagertransaction-manager-ref
JavaConfig 和 XML 哪个更好?XML 是很久以前 Spring 的配置方式。在当今快速发展的 Java、记录类型、注释等时代,新项目通常尽可能多地使用纯 Java。虽然没有立即删除 XML 支持的计划,但某些最新功能可能无法通过 XML 获得。
表 1.元素的自定义 JPA 特定属性repositories

entity-manager-factory-refSpring中文文档

显式连接要使用的存储库与元素检测到的存储库。如果应用程序中使用了多个 Bean,则通常使用。如果未配置,Spring Data 会自动查找名称为 的 Bean。EntityManagerFactoryrepositoriesEntityManagerFactoryEntityManagerFactoryentityManagerFactoryApplicationContextSpring中文文档

transaction-manager-refSpring中文文档

显式连接要使用的存储库与元素检测到的存储库。通常只有在配置了多个事务管理器或 Bean 时才需要这样做。默认为在当前 .PlatformTransactionManagerrepositoriesEntityManagerFactoryPlatformTransactionManagerApplicationContextSpring中文文档

Spring Data JPA 要求在未定义显式的情况下存在名为 bean。PlatformTransactionManagertransactionManagertransaction-manager-ref

引导模式

默认情况下,Spring Data JPA 存储库是默认的 Spring Bean。 它们是单例作用域的,并急切地初始化。 在启动过程中,它们已经与 JPA 进行交互,以进行验证和元数据分析。 Spring Framework 支持在后台线程中初始化 JPA,因为该过程通常会占用 Spring 应用程序中的大量启动时间。 为了有效地利用后台初始化,我们需要确保 JPA 存储库尽可能晚地初始化。EntityManagerEntityManagerFactorySpring中文文档

从 Spring Data JPA 2.1 开始,您现在可以配置(通过注释或 XML 命名空间)采用以下值:BootstrapMode@EnableJpaRepositoriesSpring中文文档

  • DEFAULT(default) — 除非用 . 显式注释,否则存储库会急切地实例化。 只有当没有客户机 Bean 需要存储库的实例时,lazification 才有效,因为这将需要初始化存储库 Bean。@LazySpring中文文档

  • LAZY— 隐式声明所有存储库 Bean 都是惰性的,并且还会导致要创建的惰性初始化代理注入到客户端 Bean 中。 这意味着,如果客户端 Bean 只是将实例存储在字段中,并且在初始化期间不使用存储库,则存储库不会被实例化。 存储库实例将在首次与存储库交互时初始化和验证。Spring中文文档

  • DEFERRED— 基本上与 相同的操作模式,但触发存储库初始化以响应 ,以便在应用程序完全启动之前验证存储库。LAZYContextRefreshedEventSpring中文文档

建议

如果您没有使用异步 JPA 引导程序,请坚持使用默认的引导模式。Spring中文文档

如果您异步引导 JPA,则是一个合理的默认值,因为它将确保 Spring Data JPA 引导程序仅等待安装,如果安装本身花费的时间比初始化所有其他应用程序组件的时间长。 尽管如此,它仍可确保在应用程序发出启动信号之前正确初始化和验证存储库。DEFERREDEntityManagerFactorySpring中文文档

LAZY是测试场景和本地开发的不错选择。 一旦您非常确定存储库可以正确引导,或者在测试应用程序的其他部分的情况下,对所有存储库运行验证可能会不必要地增加启动时间。 这同样适用于本地开发,在本地开发中,您仅访问可能需要初始化单个存储库的应用程序部分。Spring中文文档