diff --git a/core/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/binder/DefaultBinderFactory.java b/core/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/binder/DefaultBinderFactory.java index a570741e7..c33b4abba 100644 --- a/core/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/binder/DefaultBinderFactory.java +++ b/core/spring-cloud-stream/src/main/java/org/springframework/cloud/stream/binder/DefaultBinderFactory.java @@ -446,23 +446,6 @@ ConfigurableApplicationContext initializeBinderContextSimple(String configuratio binderProducingContext.getBeanFactory().setConversionService(this.context.getBeanFactory().getConversionService()); } - List sourceClasses = new ArrayList<>(); - sourceClasses.addAll(Arrays.asList(binderType.getConfigurationClasses())); - if (binderProperties.containsKey("spring.main.sources")) { - String sources = (String) binderProperties.get("spring.main.sources"); - if (StringUtils.hasText(sources)) { - Stream.of(sources.split(",")).forEach(source -> { - try { - sourceClasses.add(Thread.currentThread().getContextClassLoader().loadClass(source.trim())); - } - catch (Exception e) { - throw new IllegalStateException("Failed to load class " + source, e); - } - }); - } - } - - binderProducingContext.register(sourceClasses.toArray(new Class[] {})); MapPropertySource binderPropertySource = new MapPropertySource(configurationName, binderProperties); binderProducingContext.getEnvironment().getPropertySources().addFirst(binderPropertySource); binderProducingContext.setDisplayName(configurationName + "_context"); @@ -502,6 +485,23 @@ public void onApplicationEvent(ApplicationEvent event) { } } + // Register the sources classes to the specific binder context after configuring the environment property sources + List sourceClasses = new ArrayList<>(Arrays.asList(binderType.getConfigurationClasses())); + if (binderProperties.containsKey("spring.main.sources")) { + String sources = (String) binderProperties.get("spring.main.sources"); + if (StringUtils.hasText(sources)) { + Stream.of(sources.split(",")).forEach(source -> { + try { + sourceClasses.add(Thread.currentThread().getContextClassLoader().loadClass(source.trim())); + } + catch (Exception e) { + throw new IllegalStateException("Failed to load class " + source, e); + } + }); + } + } + binderProducingContext.register(sourceClasses.toArray(new Class[] {})); + if (refresh) { if (!useApplicationContextAsParent || "integration".equals(binderType.getDefaultName())) { this.propagateSharedBeans(binderProducingContext, (GenericApplicationContext) this.context);