Skip to content

Commit d4b8e38

Browse files
committedJan 28, 2021
Polishing.
Attach repository interface name to each repository init event. Make spring.data.repository.postprocessors conditional to reduce the number of events. Append full fragment diagnostics. Reorder methods, add author tag, reformat code. Closes #2247. Original pull request: #2273.
1 parent 7d7f62f commit d4b8e38

File tree

3 files changed

+75
-45
lines changed

3 files changed

+75
-45
lines changed
 

‎src/main/java/org/springframework/data/repository/config/RepositoryConfigurationDelegate.java

+9-11
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
* @author Oliver Gierke
5858
* @author Jens Schauder
5959
* @author Mark Paluch
60+
* @author Christoph Strobl
6061
*/
6162
public class RepositoryConfigurationDelegate {
6263

@@ -148,8 +149,9 @@ public List<BeanComponentDefinition> registerRepositoriesIn(BeanDefinitionRegist
148149

149150
ApplicationStartup startup = getStartup(registry);
150151
StartupStep repoScan = startup.start("spring.data.repository.scanning");
151-
repoScan.tag("data-module", extension.getModuleName());
152-
repoScan.tag("packages", configurationSource.getBasePackages().stream().collect(Collectors.joining(", ")));
152+
repoScan.tag("dataModule", extension.getModuleName());
153+
repoScan.tag("basePackages",
154+
() -> configurationSource.getBasePackages().stream().collect(Collectors.joining(", ")));
153155

154156
watch.start();
155157

@@ -263,18 +265,14 @@ private boolean multipleStoresDetected() {
263265
return multipleModulesFound;
264266
}
265267

266-
ApplicationStartup getStartup(BeanDefinitionRegistry registry) {
268+
private static ApplicationStartup getStartup(BeanDefinitionRegistry registry) {
267269

268-
if(ConfigurableBeanFactory.class.isInstance(registry)) {
269-
return ((ConfigurableBeanFactory)registry).getApplicationStartup();
270+
if (registry instanceof ConfigurableBeanFactory) {
271+
return ((ConfigurableBeanFactory) registry).getApplicationStartup();
270272
}
271273

272-
if (DefaultListableBeanFactory.class.isInstance(registry)) {
273-
return ((DefaultListableBeanFactory)registry).getBeanProvider(ApplicationStartup.class).getIfAvailable(() -> ApplicationStartup.DEFAULT);
274-
}
275-
276-
if(GenericApplicationContext.class.isInstance(registry)) {
277-
return ((GenericApplicationContext)registry).getDefaultListableBeanFactory().getApplicationStartup();
274+
if (registry instanceof GenericApplicationContext) {
275+
return ((GenericApplicationContext) registry).getDefaultListableBeanFactory().getApplicationStartup();
278276
}
279277

280278
return ApplicationStartup.DEFAULT;

‎src/main/java/org/springframework/data/repository/core/support/RepositoryFactorySupport.java

+66-33
Original file line numberDiff line numberDiff line change
@@ -277,28 +277,52 @@ public <T> T getRepository(Class<T> repositoryInterface, RepositoryFragments fra
277277

278278
ApplicationStartup applicationStartup = getStartup();
279279

280-
StartupStep repositoryInit = applicationStartup.start("spring.data.repository.init");
281-
repositoryInit.tag("repository", () -> repositoryInterface.getName());
280+
StartupStep repositoryInit = onEvent(applicationStartup, "spring.data.repository.init", repositoryInterface);
282281

283-
StartupStep repositoryMetadataStep = applicationStartup.start("spring.data.repository.metadata");
282+
repositoryBaseClass.ifPresent(it -> repositoryInit.tag("baseClass", it.getName()));
283+
284+
StartupStep repositoryMetadataStep = onEvent(applicationStartup, "spring.data.repository.metadata",
285+
repositoryInterface);
284286
RepositoryMetadata metadata = getRepositoryMetadata(repositoryInterface);
285287
repositoryMetadataStep.end();
286288

287-
StartupStep repositoryCompositionStep = applicationStartup.start("spring.data.repository.composition");
288-
repositoryCompositionStep.tag("fragment.count", () -> String.valueOf(fragments.size()));
289+
StartupStep repositoryCompositionStep = onEvent(applicationStartup, "spring.data.repository.composition",
290+
repositoryInterface);
291+
repositoryCompositionStep.tag("fragment.count", String.valueOf(fragments.size()));
289292

290293
RepositoryComposition composition = getRepositoryComposition(metadata, fragments);
291294
RepositoryInformation information = getRepositoryInformation(metadata, composition);
295+
296+
repositoryCompositionStep.tag("fragments", () -> {
297+
298+
StringBuilder fragmentsTag = new StringBuilder();
299+
300+
for (RepositoryFragment<?> fragment : composition.getFragments()) {
301+
302+
if (fragmentsTag.length() > 0) {
303+
fragmentsTag.append(";");
304+
}
305+
306+
fragmentsTag.append(fragment.getSignatureContributor().getName());
307+
fragmentsTag.append(fragment.getImplementation().map(it -> ":" + it.getClass().getName()).orElse(""));
308+
}
309+
310+
return fragmentsTag.toString();
311+
});
312+
292313
repositoryCompositionStep.end();
293314

294315
validate(information, composition);
295316

296-
StartupStep repositoryTargetStep = applicationStartup.start("spring.data.repository.target");
317+
StartupStep repositoryTargetStep = onEvent(applicationStartup, "spring.data.repository.target",
318+
repositoryInterface);
297319
Object target = getTargetRepository(information);
320+
321+
repositoryTargetStep.tag("target", target.getClass().getName());
298322
repositoryTargetStep.end();
299323

300324
// Create proxy
301-
StartupStep repositoryProxyStep = applicationStartup.start("spring.data.repository.proxy");
325+
StartupStep repositoryProxyStep = onEvent(applicationStartup, "spring.data.repository.proxy", repositoryInterface);
302326
ProxyFactory result = new ProxyFactory();
303327
result.setTarget(target);
304328
result.setInterfaces(repositoryInterface, Repository.class, TransactionalProxy.class);
@@ -309,30 +333,33 @@ public <T> T getRepository(Class<T> repositoryInterface, RepositoryFragments fra
309333

310334
result.addAdvisor(ExposeInvocationInterceptor.ADVISOR);
311335

312-
StartupStep repositoryPostprocessorsStep = applicationStartup.start("spring.data.repository.postprocessors");
313-
postProcessors.forEach(processor -> {
314-
315-
StartupStep singlePostProcessor = applicationStartup.start("spring.data.repository.postprocessor");
316-
singlePostProcessor.tag("type", processor.getClass().getName());
317-
processor.postProcess(result, information);
318-
singlePostProcessor.end();
319-
});
320-
repositoryPostprocessorsStep.end();
336+
if (!postProcessors.isEmpty()) {
337+
StartupStep repositoryPostprocessorsStep = onEvent(applicationStartup, "spring.data.repository.postprocessors",
338+
repositoryInterface);
339+
postProcessors.forEach(processor -> {
340+
341+
StartupStep singlePostProcessor = onEvent(applicationStartup, "spring.data.repository.postprocessor",
342+
repositoryInterface);
343+
singlePostProcessor.tag("type", processor.getClass().getName());
344+
processor.postProcess(result, information);
345+
singlePostProcessor.end();
346+
});
347+
repositoryPostprocessorsStep.end();
348+
}
321349

322350
if (DefaultMethodInvokingMethodInterceptor.hasDefaultMethods(repositoryInterface)) {
323351
result.addAdvice(new DefaultMethodInvokingMethodInterceptor());
324352
}
325353

326-
StartupStep queryExecutorsStep = applicationStartup.start("spring.data.repository.queryexecutors");
327354
ProjectionFactory projectionFactory = getProjectionFactory(classLoader, beanFactory);
328355
Optional<QueryLookupStrategy> queryLookupStrategy = getQueryLookupStrategy(queryLookupStrategyKey,
329356
evaluationContextProvider);
330357
result.addAdvice(new QueryExecutorMethodInterceptor(information, projectionFactory, queryLookupStrategy,
331358
namedQueries, queryPostProcessors, methodInvocationListeners));
332-
queryExecutorsStep.end();
333359

334-
composition = composition.append(RepositoryFragment.implemented(target));
335-
result.addAdvice(new ImplementationMethodExecutionInterceptor(information, composition, methodInvocationListeners));
360+
RepositoryComposition compositionToUse = composition.append(RepositoryFragment.implemented(target));
361+
result.addAdvice(
362+
new ImplementationMethodExecutionInterceptor(information, compositionToUse, methodInvocationListeners));
336363

337364
T repository = (T) result.getProxy(classLoader);
338365
repositoryProxyStep.end();
@@ -346,19 +373,6 @@ public <T> T getRepository(Class<T> repositoryInterface, RepositoryFragments fra
346373
return repository;
347374
}
348375

349-
ApplicationStartup getStartup() {
350-
351-
try {
352-
353-
ApplicationStartup applicationStartup = beanFactory != null ? beanFactory.getBean(ApplicationStartup.class)
354-
: ApplicationStartup.DEFAULT;
355-
356-
return applicationStartup != null ? applicationStartup : ApplicationStartup.DEFAULT;
357-
} catch (NoSuchBeanDefinitionException e) {
358-
return ApplicationStartup.DEFAULT;
359-
}
360-
}
361-
362376
/**
363377
* Returns the {@link ProjectionFactory} to be used with the repository instances created.
364378
*
@@ -540,6 +554,25 @@ protected final <R> R getTargetRepositoryViaReflection(Class<?> baseClass, Objec
540554
baseClass, Arrays.stream(constructorArguments).map(Object::getClass).collect(Collectors.toList()))));
541555
}
542556

557+
private ApplicationStartup getStartup() {
558+
559+
try {
560+
561+
ApplicationStartup applicationStartup = beanFactory != null ? beanFactory.getBean(ApplicationStartup.class)
562+
: ApplicationStartup.DEFAULT;
563+
564+
return applicationStartup != null ? applicationStartup : ApplicationStartup.DEFAULT;
565+
} catch (NoSuchBeanDefinitionException e) {
566+
return ApplicationStartup.DEFAULT;
567+
}
568+
}
569+
570+
private StartupStep onEvent(ApplicationStartup applicationStartup, String name, Class<?> repositoryInterface) {
571+
572+
StartupStep step = applicationStartup.start(name);
573+
return step.tag("repository", repositoryInterface.getName());
574+
}
575+
543576
/**
544577
* Method interceptor that calls methods on the {@link RepositoryComposition}.
545578
*

‎src/test/java/org/springframework/data/repository/core/support/RepositoryFactorySupportUnitTests.java

-1
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,6 @@ void callsApplicationStartupOnRepositoryInitialization() {
420420
orderedInvocation.verify(startup).start("spring.data.repository.composition");
421421
orderedInvocation.verify(startup).start("spring.data.repository.target");
422422
orderedInvocation.verify(startup).start("spring.data.repository.proxy");
423-
orderedInvocation.verify(startup).start("spring.data.repository.postprocessors");
424423
}
425424

426425
private ConvertingRepository prepareConvertingRepository(final Object expectedValue) {

0 commit comments

Comments
 (0)
Please sign in to comment.