Spring Data Jpa多个@EnableJpaRepositories,BootstrapMode.DEFERRED不能正常工作

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring Data Jpa多个@EnableJpaRepositories,BootstrapMode.DEFERRED不能正常工作相关的知识,希望对你有一定的参考价值。

我的Spring Boot应用程序中有两个DataSource bean,还有两个LocalContainerEntityManagerFactoryBean bean,用于为每个bean配置Jpa和Hibernate。因此我有2个Spring Java Configs。

在升级到Spring Boot 2.1.0之后,我想为两种配置启用新的BootstrapMode BootstrapMode.DEFERRED,但是我的应用程序无法在我的StartupTests中启动,也无法在真正启动应用程序时启动。

LOG

main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFERRED mode.
main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 394ms. Found 14 repository interfaces.
main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFERRED mode.
main] o.s.boot.SpringApplication               : Application run failed

例外

java.lang.IllegalStateException: Could not register object
 [org.springframework.data.repository.config.DeferredRepositoryInitializationListener@422ab737]
 under bean name
 'org.springframework.data.repository.config.DeferredRepositoryInitializationListener':
 there is already object
 [org.springframework.data.repository.config.DeferredRepositoryInitializationListener@3fe512d2]
 bound  at
 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.registerSingleton(DefaultSingletonBeanRegistry.java:120)
    at
 org.springframework.beans.factory.support.DefaultListableBeanFactory.registerSingleton(DefaultListableBeanFactory.java:1027)
    at
 org.springframework.data.repository.config.RepositoryConfigurationDelegate.potentiallyLazifyRepositories(RepositoryConfigurationDelegate.java:226)
    at
 org.springframework.data.repository.config.RepositoryConfigurationDelegate.registerRepositoriesIn(RepositoryConfigurationDelegate.java:177)
    at
 org.springframework.data.repository.config.RepositoryBeanDefinitionRegistrarSupport.registerBeanDefinitions(RepositoryBeanDefinitionRegistrarSupport.java:85)
    at
 org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.lambda$loadBeanDefinitionsFromRegistrars$1(ConfigurationClassBeanDefinitionReader.java:364)
    at java.base/java.util.LinkedHashMap.forEach(LinkedHashMap.java:684)
    at
 org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsFromRegistrars(ConfigurationClassBeanDefinitionReader.java:363)
    at
 org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForConfigurationClass(ConfigurationClassBeanDefinitionReader.java:145)
    at
 org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitions(ConfigurationClassBeanDefinitionReader.java:117)
    at
 org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:327)
    at
 org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:232)
    at
 org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:275)
    at
 org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:95)
    at
 org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:691)
    at
 org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:528)
    at
 org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140)
    at
 org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775)
    at
 org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
    at
 org.springframework.boot.SpringApplication.run(SpringApplication.java:316)
    at
 org.springframework.boot.SpringApplication.run(SpringApplication.java:1260)
    at
 org.springframework.boot.SpringApplication.run(SpringApplication.java:1248)

当只有一个@EnableJpaRepositories被设置为BootstrapMode.DEFERRED而另一个被设置为BootstrapMode.DEFAULT时,我设法获得应用程序启动。

所以我的问题是:是否可以使用@EnableJpaRepositories配置多个Spring配置,并将BootstrapMode设置为DEFERRED

答案

设置BootstrapMode.LAZY正在使用多个EMF。我在DEFERRED模式下遇到了同样的问题。

以上是关于Spring Data Jpa多个@EnableJpaRepositories,BootstrapMode.DEFERRED不能正常工作的主要内容,如果未能解决你的问题,请参考以下文章

多个数据库的 Spring Data JPA 配置

Spring Data JPA NamedStoredProcedureQuery 多个输出参数

Spring Data JPA 中的多个基础存储库

管理来自多个表的数据 Spring data JPA

使用 Spring Data JPA 的多个数据库

Spring Data Jpa多个@EnableJpaRepositories,BootstrapMode.DEFERRED不能正常工作