Skip to content

Commit 9f4cced

Browse files
committed
[kafka][metrics] Fix bean conditional resolution order to prioritize user-defined beans over binder's conditional beans in a multi-binder scenario
This change modifies the loading order in DefaultBinderFactory.initializeBinderContextSimple to ensure that user configuration classes specified via spring.main.sources are loaded before binder configuration classes. This allows @ConditionalOnMissingBean annotations in the binder configurations to properly detect user-provided beans. Previously, when using KafkaBinderConfiguration with @ConditionalOnMissingBean(KafkaBinderMetrics.class), even if the user had configured a custom KafkaBinderMetrics bean via spring.main.sources, the condition would not work correctly because binder classes were loaded first. Fixes gh-3114 Signed-off-by: ferblaca <[email protected]>
1 parent e5d0d14 commit 9f4cced

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

core/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/binder/DefaultBinderFactory.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -510,8 +510,10 @@ public void onApplicationEvent(ApplicationEvent event) {
510510
}
511511
}
512512

513-
// Register the sources classes to the specific binder context after configuring the environment property sources
514-
List<Class> sourceClasses = new ArrayList<>(Arrays.asList(binderType.getConfigurationClasses()));
513+
// Modified registration: first user classes (spring.main.sources) and then binder classes
514+
List<Class> sourceClasses = new ArrayList<>();
515+
516+
// First register user classes if defined in spring.main.sources
515517
if (binderProperties.containsKey("spring.main.sources")) {
516518
String sources = (String) binderProperties.get("spring.main.sources");
517519
if (StringUtils.hasText(sources)) {
@@ -525,6 +527,10 @@ public void onApplicationEvent(ApplicationEvent event) {
525527
});
526528
}
527529
}
530+
531+
// Then add binder configuration classes
532+
sourceClasses.addAll(Arrays.asList(binderType.getConfigurationClasses()));
533+
528534
binderProducingContext.register(sourceClasses.toArray(new Class[] {}));
529535

530536
if (refresh) {

0 commit comments

Comments
 (0)