具有BootstrapMode.DEFERRED的Spring Data Jpa多个@EnableJpaRepositories无法正常工作

时间:2018-11-07 08:09:28

标签: java spring hibernate spring-boot spring-data-jpa

我的Spring Boot应用程序中有两个DataSource bean,还有两个LocalContainerEntityManagerFactoryBean bean,分别为它们配置Jpa和Hibernate。因此,我为他们提供了2个Spring Java Config。

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

日志

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并且将两个BootstrapMode都设置为DEFERRED来配置多个Spring Configurations?

1 个答案:

答案 0 :(得分:0)

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