Skip to content

Prevent cache pollution by storing only the factories #34732

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -101,7 +101,7 @@ public class SpringFactoriesLoader {

private static final Log logger = LogFactory.getLog(SpringFactoriesLoader.class);

static final Map<ClassLoader, Map<String, SpringFactoriesLoader>> cache = new ConcurrentReferenceHashMap<>();
static final Map<ClassLoader, Map<String, Factories>> cache = new ConcurrentReferenceHashMap<>();


@Nullable
Expand Down Expand Up @@ -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<String, SpringFactoriesLoader> loaders = cache.computeIfAbsent(
Map<String, Factories> 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<String, List<String>> loadFactoriesResource(ClassLoader classLoader, String resourceLocation) {
Expand Down Expand Up @@ -673,4 +674,8 @@ static FailureHandler handleMessage(BiConsumer<Supplier<String>, Throwable> mess
}
}


private record Factories(Map<String, List<String>> byType) {

}
}
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
Loading