Skip to content

Commit 4af0ee2

Browse files
committed
Work around Spring Framework cache pollution bug
Update `SpringApplication` to work around `SpringFactoriesLoader` cache pollution by loading factories using a `null` class loader. See spring-projects/spring-framework#34732 for details.
1 parent 92a8d41 commit 4af0ee2

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

Diff for: spring-boot-project/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -480,7 +480,7 @@ private <T> List<T> getSpringFactoriesInstances(Class<T> type) {
480480
}
481481

482482
private <T> List<T> getSpringFactoriesInstances(Class<T> type, ArgumentResolver argumentResolver) {
483-
return SpringFactoriesLoader.forDefaultResourceLocation(getClassLoader()).load(type, argumentResolver);
483+
return SpringFactoriesLoader.forDefaultResourceLocation(getClassLoader(null)).load(type, argumentResolver);
484484
}
485485

486486
private ConfigurableEnvironment getOrCreateEnvironment() {
@@ -715,10 +715,11 @@ public ResourceLoader getResourceLoader() {
715715
* @return a ClassLoader (never null)
716716
*/
717717
public ClassLoader getClassLoader() {
718-
if (this.resourceLoader != null) {
719-
return this.resourceLoader.getClassLoader();
720-
}
721-
return ClassUtils.getDefaultClassLoader();
718+
return getClassLoader(ClassUtils.getDefaultClassLoader());
719+
}
720+
721+
private ClassLoader getClassLoader(ClassLoader fallback) {
722+
return (this.resourceLoader != null) ? this.resourceLoader.getClassLoader() : fallback;
722723
}
723724

724725
/**

0 commit comments

Comments
 (0)