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:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user