Skip to content

Commit

Permalink
Change order of the registry of sources classes after configuring Env…
Browse files Browse the repository at this point in the history
…ironment with all propertySources, so that sources classes can resolve conditions configurations. Fixes GH-3031

Resolves #3032
  • Loading branch information
Fernando Blanch authored and olegz committed Nov 18, 2024
1 parent 286d686 commit 9fc31b3
Showing 1 changed file with 17 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -438,23 +438,6 @@ ConfigurableApplicationContext initializeBinderContextSimple(String configuratio
binderProducingContext.getBeanFactory().setConversionService(this.context.getBeanFactory().getConversionService());
}

List<Class> 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");
Expand Down Expand Up @@ -494,6 +477,23 @@ public void onApplicationEvent(ApplicationEvent event) {
}
}

// Register the sources classes to the specific binder context after configuring the environment property sources
List<Class> 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);
Expand Down

0 comments on commit 9fc31b3

Please sign in to comment.