Skip to content

Commit 0cb1cca

Browse files
committed
DATACMNS-609 - Improved bean definition registration for repository configuration.
The bean definitions that were registered for a repository configuration setup we registered once for every usage of the repository configuration element (annotation or XML). This caused multiple registrations of the very same bean definition which - as in case of the RepositoryInterfaceAwareBeanPostProcessor - apparently leads to performance problems in the container startup. Feedback for the latter claim is already asked for but we improved the registration setup nonetheless. Introduced a registerIfNotAlreadyRegistered(…) method on RepositoryConfigurationExtensionSupport to allow easy registration of to-b-registered-once-and-only-once beans. We now hint towards the newly introduced method in registerWithSourceAndGeneratedBeanName(…).
1 parent 447aca6 commit 0cb1cca

File tree

2 files changed

+57
-6
lines changed

2 files changed

+57
-6
lines changed

src/main/java/org/springframework/data/repository/config/RepositoryConfigurationExtensionSupport.java

+29-6
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public void registerBeansForRoot(BeanDefinitionRegistry registry, RepositoryConf
118118
AbstractBeanDefinition definition = BeanDefinitionBuilder.rootBeanDefinition(REPOSITORY_INTERFACE_POST_PROCESSOR)
119119
.getBeanDefinition();
120120

121-
registerWithSourceAndGeneratedBeanName(registry, definition, configurationSource.getSource());
121+
registerIfNotAlreadyRegistered(definition, registry, REPOSITORY_INTERFACE_POST_PROCESSOR, configurationSource);
122122
}
123123

124124
/**
@@ -169,12 +169,15 @@ protected Collection<Class<?>> getIdentifyingTypes() {
169169

170170
/**
171171
* Sets the given source on the given {@link AbstractBeanDefinition} and registers it inside the given
172-
* {@link BeanDefinitionRegistry}.
172+
* {@link BeanDefinitionRegistry}. For {@link BeanDefinition}s to be registerd once-and-only-once for all
173+
* configuration elements (annotation or XML), prefer calling
174+
* {@link #registerIfNotAlreadyRegistered(AbstractBeanDefinition, BeanDefinitionRegistry, String, Object)} with a
175+
* dedicated bean name to avoid the bead definition being registered multiple times. *
173176
*
174-
* @param registry
175-
* @param bean
176-
* @param source
177-
* @return
177+
* @param registry must not be {@literal null}.
178+
* @param bean must not be {@literal null}.
179+
* @param source must not be {@literal null}.
180+
* @return the bean name generated for the given {@link BeanDefinition}
178181
*/
179182
public static String registerWithSourceAndGeneratedBeanName(BeanDefinitionRegistry registry,
180183
AbstractBeanDefinition bean, Object source) {
@@ -187,6 +190,26 @@ public static String registerWithSourceAndGeneratedBeanName(BeanDefinitionRegist
187190
return beanName;
188191
}
189192

193+
/**
194+
* Registers the given {@link AbstractBeanDefinition} with the given registry with the given bean name unless the
195+
* registry already contains a bean with that name.
196+
*
197+
* @param bean must not be {@literal null}.
198+
* @param registry must not be {@literal null}.
199+
* @param beanName must not be {@literal null} or empty.
200+
* @param source must not be {@literal null}.
201+
*/
202+
public static void registerIfNotAlreadyRegistered(AbstractBeanDefinition bean, BeanDefinitionRegistry registry,
203+
String beanName, Object source) {
204+
205+
if (registry.containsBeanDefinition(beanName)) {
206+
return;
207+
}
208+
209+
bean.setSource(source);
210+
registry.registerBeanDefinition(beanName, bean);
211+
}
212+
190213
/**
191214
* Returns whether the given {@link BeanDefinitionRegistry} already contains a bean of the given type assuming the
192215
* bean name has been autogenerated.

src/test/java/org/springframework/data/repository/config/RepositoryConfigurationExtensionSupportUnitTests.java

+28
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,12 @@
2323
import java.util.Collections;
2424

2525
import org.junit.Test;
26+
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
2627
import org.springframework.context.annotation.Primary;
28+
import org.springframework.core.env.StandardEnvironment;
29+
import org.springframework.core.io.DefaultResourceLoader;
30+
import org.springframework.core.type.AnnotationMetadata;
31+
import org.springframework.core.type.StandardAnnotationMetadata;
2732
import org.springframework.data.repository.Repository;
2833
import org.springframework.data.repository.core.support.RepositoryFactorySupport;
2934

@@ -60,6 +65,26 @@ public void considersRepositoryInterfaceExtendingStoreInterfaceStrictMatch() {
6065
assertThat(extension.isStrictRepositoryCandidate(ExtendingInterface.class), is(true));
6166
}
6267

68+
/**
69+
* @see DATACMNS-609
70+
*/
71+
@Test
72+
public void registersRepositoryInterfaceAwareBeanPostProcessorOnlyOnceForMultipleConfigurations() {
73+
74+
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
75+
AnnotationMetadata annotationMetadata = new StandardAnnotationMetadata(SampleConfiguration.class, true);
76+
77+
DefaultResourceLoader resourceLoader = new DefaultResourceLoader();
78+
StandardEnvironment environment = new StandardEnvironment();
79+
AnnotationRepositoryConfigurationSource configurationSource = new AnnotationRepositoryConfigurationSource(
80+
annotationMetadata, EnableRepositories.class, resourceLoader, environment);
81+
82+
extension.registerBeansForRoot(beanFactory, configurationSource);
83+
extension.registerBeansForRoot(beanFactory, configurationSource);
84+
85+
assertThat(beanFactory.getBeanDefinitionCount(), is(1));
86+
}
87+
6388
static class SampleRepositoryConfigurationExtension extends RepositoryConfigurationExtensionSupport {
6489

6590
@Override
@@ -95,4 +120,7 @@ interface PlainTypeRepository extends Repository<PlainType, Long> {}
95120
interface StoreInterface {}
96121

97122
interface ExtendingInterface extends StoreInterface, Repository<PlainType, Long> {}
123+
124+
@EnableRepositories
125+
static class SampleConfiguration {}
98126
}

0 commit comments

Comments
 (0)