Spring Bean Overriding在JpaAuditingRegistrar中引发错误 [英] Spring Bean Overriding throws error in JpaAuditingRegistrar
问题描述
默认情况下不允许切换到Spring-Boot 2.1.0.RELEASE bean覆盖.这将导致以下错误:
Switching to Spring-Boot 2.1.0.RELEASE bean overriding is not allowed by default anymore. This causes following error:
...
Caused by: org.springframework.beans.factory.support.BeanDefinitionOverrideException: Invalid bean definition with name 'org.springframework.context.config.internalBeanConfigurerAspect' defined in null: Cannot register bean definition [Root bean: class [org.springframework.beans.factory.aspectj.AnnotationBeanConfigurerAspect]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=aspectOf; initMethodName=null; destroyMethodName=null] for bean 'org.springframework.context.config.internalBeanConfigurerAspect': There is already [Root bean: class [org.springframework.beans.factory.aspectj.AnnotationBeanConfigurerAspect]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=aspectOf; initMethodName=null; destroyMethodName=null] bound.
at org.springframework.beans.factory.support.DefaultListableBeanFactory.registerBeanDefinition(DefaultListableBeanFactory.java:894)
at org.springframework.data.jpa.repository.config.JpaAuditingRegistrar.registerBeanConfigurerAspectIfNecessary(JpaAuditingRegistrar.java:135)
at org.springframework.data.jpa.repository.config.JpaAuditingRegistrar.registerBeanDefinitions(JpaAuditingRegistrar.java:88)
at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.lambda$loadBeanDefinitionsFromRegistrars$1(ConfigurationClassBeanDefinitionReader.java:364)
at java.util.LinkedHashMap.forEach(Unknown Source)
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.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.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:127)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:99)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:117)
... 25 more
通过一些调试,我发现org.springframework.data.jpa.repository.config.JpaAuditingRegistrar.registerBeanConfigurerAspectIfNecessary(BeanDefinitionRegistry)
将被调用两次.如果有人可以解释一下这个问题,那就太好了.无论如何,该方法中的第一个if语句应识别出AnnotationBeanConfigurerAspect
类型的Bean已经注册,并且 not 不会导致OverrideException:
With some debugging I found that org.springframework.data.jpa.repository.config.JpaAuditingRegistrar.registerBeanConfigurerAspectIfNecessary(BeanDefinitionRegistry)
will be called twice. Would be nice if someone could explain this btw. Anyway the first if-statement in that method should recognize the bean of type AnnotationBeanConfigurerAspect
is already registered and does not lead to an OverrideException:
private void registerBeanConfigurerAspectIfNecessary(BeanDefinitionRegistry registry) {
if (registry.containsBeanDefinition(BEAN_CONFIGURER_ASPECT_CLASS_NAME)) {
return;
}
registry.containsBeanDefinition
检查registry.definitionMap
中的key
是否为org.springframework.beans.factory.aspectj.AnnotationBeanConfigurerAspect
.但是BeanDefinition
看起来像这样:
registry.containsBeanDefinition
checks the key
in registry.definitionMap
for org.springframework.beans.factory.aspectj.AnnotationBeanConfigurerAspect
. But the BeanDefinition
looks like this:
key = org.springframework.context.config.internalBeanConfigurerAspect
value = Bean of Type [org.springframework.beans.factory.aspectj.AnnotationBeanConfigurerAspect]
key
和value
似乎混在一起了.
推荐答案
失败是由 bug 中,已修复但尚未发布.它将是Spring Data Lovelace SR3的一部分,它将包含在Spring Boot 2.1.1中.
The failure is caused by a bug in Spring Data JPA that has been fixed but not yet released. It will be part of Spring Data Lovelace SR3 which will be included in Spring Boot 2.1.1.
这篇关于Spring Bean Overriding在JpaAuditingRegistrar中引发错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!