Fix SpringFactoriesLoader cache key when using default classloader

Update `SpringFactoriesLoader` so that `null` is never used for the
cache key. Prior to this commit, calling `forDefaultResourceLocation`
with `null` and `ClassUtils.getDefaultClassLoader()` would provide
different `SpringFactoriesLoader` instances rather than making use
of a single shared cached instance.

See gh-28416
This commit is contained in:
Phillip Webb
2022-05-10 10:42:29 -07:00
parent eb50a6f4a0
commit 4cebd9d392
2 changed files with 44 additions and 34 deletions

View File

@@ -34,6 +34,7 @@ import org.springframework.core.io.support.SpringFactoriesLoader.ArgumentResolve
import org.springframework.core.io.support.SpringFactoriesLoader.FactoryInstantiator;
import org.springframework.core.io.support.SpringFactoriesLoader.FailureHandler;
import org.springframework.core.log.LogMessage;
import org.springframework.util.ClassUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
@@ -169,6 +170,13 @@ class SpringFactoriesLoaderTests {
assertThat(factories.get(0)).isInstanceOf(MyDummyFactory1.class);
}
@Test
void sameCachedResultIsUsedForDefaultClassLoaderAndNullClassLoader() {
SpringFactoriesLoader forNull = SpringFactoriesLoader.forDefaultResourceLocation(null);
SpringFactoriesLoader forDefault = SpringFactoriesLoader.forDefaultResourceLocation(ClassUtils.getDefaultClassLoader());
assertThat(forNull).isSameAs(forDefault);
}
@Nested
class FailureHandlerTests {