diff --git a/spring-core/src/main/java/org/springframework/core/io/support/SpringFactoriesLoader.java b/spring-core/src/main/java/org/springframework/core/io/support/SpringFactoriesLoader.java index 59c583356a6c..861ae9fe1020 100644 --- a/spring-core/src/main/java/org/springframework/core/io/support/SpringFactoriesLoader.java +++ b/spring-core/src/main/java/org/springframework/core/io/support/SpringFactoriesLoader.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2023 the original author or authors. + * Copyright 2002-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -101,7 +101,7 @@ public class SpringFactoriesLoader { private static final Log logger = LogFactory.getLog(SpringFactoriesLoader.class); - static final Map> cache = new ConcurrentReferenceHashMap<>(); + static final Map> cache = new ConcurrentReferenceHashMap<>(); @Nullable @@ -327,10 +327,11 @@ public static SpringFactoriesLoader forResourceLocation(String resourceLocation, Assert.hasText(resourceLocation, "'resourceLocation' must not be empty"); ClassLoader resourceClassLoader = (classLoader != null ? classLoader : SpringFactoriesLoader.class.getClassLoader()); - Map loaders = cache.computeIfAbsent( + Map factoriesCache = cache.computeIfAbsent( resourceClassLoader, key -> new ConcurrentReferenceHashMap<>()); - return loaders.computeIfAbsent(resourceLocation, key -> - new SpringFactoriesLoader(classLoader, loadFactoriesResource(resourceClassLoader, resourceLocation))); + Factories factories = factoriesCache.computeIfAbsent(resourceLocation, key -> + new Factories(loadFactoriesResource(resourceClassLoader, resourceLocation))); + return new SpringFactoriesLoader(classLoader, factories.byType()); } protected static Map> loadFactoriesResource(ClassLoader classLoader, String resourceLocation) { @@ -673,4 +674,8 @@ static FailureHandler handleMessage(BiConsumer, Throwable> mess } } + + private record Factories(Map> byType) { + + } } diff --git a/spring-core/src/test/java/org/springframework/core/io/support/SpringFactoriesLoaderTests.java b/spring-core/src/test/java/org/springframework/core/io/support/SpringFactoriesLoaderTests.java index 06a452932138..19864545937b 100644 --- a/spring-core/src/test/java/org/springframework/core/io/support/SpringFactoriesLoaderTests.java +++ b/spring-core/src/test/java/org/springframework/core/io/support/SpringFactoriesLoaderTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2024 the original author or authors. + * Copyright 2002-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,6 +25,7 @@ import java.util.List; import org.apache.commons.logging.Log; +import org.assertj.core.extractor.Extractors; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Nested; @@ -166,9 +167,26 @@ void loadForResourceLocationLoadsFactories() { void sameCachedResultIsUsedForDefaultClassLoaderAndNullClassLoader() { SpringFactoriesLoader forNull = SpringFactoriesLoader.forDefaultResourceLocation(null); SpringFactoriesLoader forDefault = SpringFactoriesLoader.forDefaultResourceLocation(ClassUtils.getDefaultClassLoader()); - assertThat(forNull).isSameAs(forDefault); + assertThat(forNull).extracting("factories").isSameAs(Extractors.byName("factories").apply(forDefault)); } + @Test + void staleClassLoaderIsUsedWithCachedResult() { + ClassLoader defaultClassLoader = ClassUtils.getDefaultClassLoader(); + ClassLoader cl1 = new ClassLoader() { + }; + SpringFactoriesLoader factories1 = SpringFactoriesLoader.forDefaultResourceLocation(defaultClassLoader); + assertThat(factories1).extracting("classLoader").isEqualTo(defaultClassLoader); + ClassLoader previousClassLoader = Thread.currentThread().getContextClassLoader(); + try { + Thread.currentThread().setContextClassLoader(cl1); + SpringFactoriesLoader factories2 = SpringFactoriesLoader.forDefaultResourceLocation(null); + assertThat(factories2).extracting("classLoader").isNull(); + } + finally { + Thread.currentThread().setContextClassLoader(previousClassLoader); + } + } @Nested class FailureHandlerTests {