Skip to content

feat(core): single registerSingleton to avoid duplication#17

Merged
rieckpil merged 1 commit into
PragmaTech-GmbH:mainfrom
seregamorph:singleton-registration
Aug 11, 2025
Merged

feat(core): single registerSingleton to avoid duplication#17
rieckpil merged 1 commit into
PragmaTech-GmbH:mainfrom
seregamorph:singleton-registration

Conversation

@seregamorph

Copy link
Copy Markdown
Contributor

There is a scenario when ContextDiagnosticApplicationInitializer is specified in both spring.factories discovered in the classpath and specified explicitly on the class. In this case test execution fails with

19:23:47 [main] ERROR org.springframework.boot.SpringApplication - Application run failed
java.lang.IllegalStateException: Could not register object [ContextDiagnostic[contextLoadStartTime=1754853753835, contextLoadEndTime=1754853827194, contextLoadComplete=true, heapMemoryUsedBytes=134002912, nonHeapMemoryUsedBytes=60453496, availableProcessors=12, totalMemoryBytes=226492416, maxMemoryBytes=6442450944, freeMemoryBytes=92055928]] under bean name 'contextDiagnostic': there is already object [ContextDiagnostic[contextLoadStartTime=1754853753830, contextLoadEndTime=1754853827194, contextLoadComplete=true, heapMemoryUsedBytes=134002912, nonHeapMemoryUsedBytes=60432104, availableProcessors=12, totalMemoryBytes=226492416, maxMemoryBytes=6442450944, freeMemoryBytes=92055928]] bound
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.registerSingleton(DefaultSingletonBeanRegistry.java:124)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.registerSingleton(DefaultListableBeanFactory.java:1175)
	at digital.pragmatech.testing.diagnostic.ContextDiagnosticApplicationInitializer.lambda$initialize$0(ContextDiagnosticApplicationInitializer.java:34)

and this failure is pretty unclear as test eventually fails with other diagnostics (if the suite is heavy and big). This simple fix makes the singleton registration conditional to do it not more than once.

@seregamorph
seregamorph force-pushed the singleton-registration branch from d6c85b3 to 44ae9a2 Compare August 10, 2025 20:16
@sonarqubecloud

Copy link
Copy Markdown

}

@Override
public boolean equals(Object that) {

@seregamorph seregamorph Aug 10, 2025

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alternatively, this could be implemented as

if (!applicationContext.getBeanFactory().containsSingleton(BEAN_CONTEXT_DIAGNOSTIC)) {
  applicationContext
      .getBeanFactory()
      .registerSingleton(BEAN_CONTEXT_DIAGNOSTIC, completedDiagnostic);
}

but this solution with equals looks as a proper solution

@seregamorph seregamorph changed the title Conditional registerSingleton to avoid duplication Single registerSingleton to avoid duplication Aug 10, 2025
@rieckpil rieckpil changed the title Single registerSingleton to avoid duplication feat(core): single registerSingleton to avoid duplication Aug 11, 2025
@rieckpil

Copy link
Copy Markdown
Contributor

good catch, thank you!

@rieckpil
rieckpil merged commit face115 into PragmaTech-GmbH:main Aug 11, 2025
1 check passed
@seregamorph
seregamorph deleted the singleton-registration branch August 11, 2025 17:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants