配置

本节介绍如何通过以下两种方式配置 Spring Data JPA:

基于注解的配置

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

示例 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;
  }
}
您必须创建 LocalContainerEntityManagerFactoryBean 而不是直接创建 EntityManagerFactory,因为前者除了创建 EntityManagerFactory 外,还参与异常转换机制。

上述配置类使用 spring-jdbcEmbeddedDatabaseBuilder API 设置了一个嵌入式 HSQL 数据库。Spring Data 然后设置一个 EntityManagerFactory 并使用 Hibernate 作为示例持久性提供者。此处声明的最后一个基础设施组件是 JpaTransactionManager。最后,该示例通过使用 @EnableJpaRepositories 注解激活 Spring Data JPA 仓库,该注解本质上与 XML 命名空间具有相同的属性。如果未配置基本包,它将使用配置类所在的包。

Spring 命名空间

Spring Data 的 JPA 模块包含一个自定义命名空间,允许定义仓库 bean。它还包含一些 JPA 特有的功能和元素属性。通常,JPA 仓库可以使用 repositories 元素设置,示例如下:

示例 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 使用。

使用 repositories 元素它为所有使用 @Repository 注解的 bean 激活持久性异常转换,以便 JPA 持久性提供者抛出的异常可以转换为 Spring 的 DataAccessException 层次结构。

自定义命名空间属性

除了 repositories 元素的默认属性外,JPA 命名空间还提供其他属性,让您对仓库的设置进行更详细的控制

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

entity-manager-factory-ref

显式连接将与 repositories 元素检测到的仓库一起使用的 EntityManagerFactory。通常在应用程序中使用多个 EntityManagerFactory bean 时使用。如果未配置,Spring Data 会自动在 ApplicationContext 中查找名为 entityManagerFactoryEntityManagerFactory bean。

transaction-manager-ref

显式连接将与 repositories 元素检测到的仓库一起使用的 PlatformTransactionManager。通常仅在配置了多个事务管理器或 EntityManagerFactory bean 时才需要。默认为当前 ApplicationContext 中定义的单个 PlatformTransactionManager

如果未定义显式 transaction-manager-ref,Spring Data JPA 需要存在名为 transactionManagerPlatformTransactionManager bean。

引导模式

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

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

  • DEFAULT (默认) — 除非明确使用 @Lazy 注解,否则仓库将急切实例化。懒加载仅在没有客户端 bean 需要仓库实例时才生效,因为这将需要初始化仓库 bean。

  • LAZY — 隐式地将所有仓库 bean 声明为懒加载,并创建懒加载初始化代理以注入客户端 bean。这意味着,如果客户端 bean 只是将实例存储在字段中,并且在初始化期间不使用仓库,则仓库将不会实例化。仓库实例将在与仓库首次交互时进行初始化和验证。

  • DEFERRED — 与 LAZY 的操作模式基本相同,但通过响应 ContextRefreshedEvent 触发仓库初始化,以便在应用程序完全启动之前验证仓库。

建议

如果您不使用异步 JPA 引导,请坚持使用默认的引导模式。

如果您异步引导 JPA,DEFERRED 是一个合理的默认值,因为它将确保 Spring Data JPA 引导仅在 EntityManagerFactory 设置本身花费的时间比初始化所有其他应用程序组件更长时才等待。尽管如此,它仍确保在应用程序发出启动信号之前,仓库已正确初始化和验证。

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

© . This site is unofficial and not affiliated with VMware.