DATAJPA-1448 - Properly avoid double registration of AnnotationBeanConfigurerAspect.
Previously we used the aspect's class name to guard the registration of a AnnotationBeanConfigurerAspect bean but eventually ended up registering it under the globally used name (org.springframework.context.config.internalBeanConfigurerAspect). That caused our bean being registered even if there already was one in the context causing the original bean to be overridden. The latter is now disabled by default in Spring Boot 2.1 so that it causes applications failing to start. We now properly check for the correct bean name to guard the registration.
This commit is contained in:
@@ -116,7 +116,7 @@ class JpaAuditingRegistrar extends AuditingBeanDefinitionRegistrarSupport {
|
||||
*/
|
||||
private void registerBeanConfigurerAspectIfNecessary(BeanDefinitionRegistry registry) {
|
||||
|
||||
if (registry.containsBeanDefinition(BEAN_CONFIGURER_ASPECT_CLASS_NAME)) {
|
||||
if (registry.containsBeanDefinition(BEAN_CONFIGURER_ASPECT_BEAN_NAME)) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -15,12 +15,18 @@
|
||||
*/
|
||||
package org.springframework.data.jpa.repository.config;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||
import org.springframework.core.type.AnnotationMetadata;
|
||||
import org.springframework.core.type.classreading.MetadataReader;
|
||||
import org.springframework.core.type.classreading.SimpleMetadataReaderFactory;
|
||||
import org.springframework.data.jpa.domain.support.AuditingBeanFactoryPostProcessor;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link JpaAuditingRegistrar}.
|
||||
@@ -44,4 +50,27 @@ public class JpaAuditingRegistrarUnitTests {
|
||||
public void rejectsNullBeanDefinitionRegistry() {
|
||||
registrar.registerBeanDefinitions(metadata, null);
|
||||
}
|
||||
|
||||
@Test // DATAJPA-1448
|
||||
public void doesNotRegisterBeanConfigurerTwice() throws Exception {
|
||||
|
||||
SimpleMetadataReaderFactory factory = new SimpleMetadataReaderFactory();
|
||||
MetadataReader reader = factory.getMetadataReader(Sample.class.getName());
|
||||
AnnotationMetadata annotationMetadata = reader.getAnnotationMetadata();
|
||||
|
||||
// Given a bean already present
|
||||
String beanName = AuditingBeanFactoryPostProcessor.BEAN_CONFIGURER_ASPECT_BEAN_NAME;
|
||||
when(registry.containsBeanDefinition(beanName)).thenReturn(true);
|
||||
|
||||
// When invoking configuration
|
||||
registrar.registerBeanDefinitions(annotationMetadata, registry);
|
||||
|
||||
// Then the bean is not registered again
|
||||
verify(registry, times(0)).registerBeanDefinition(eq(beanName), any());
|
||||
|
||||
registrar.registerBeanDefinitions(annotationMetadata, registry);
|
||||
}
|
||||
|
||||
@EnableJpaAuditing
|
||||
static class Sample {}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user