Skip to content

Commit 07ef7a9

Browse files
philwebbsnicoll
authored andcommitted
Cache SpringFactoriesLoader loads
Update `SpringFactoriesLoader` to cache property file loads. This helps reduce the number of garbage objects created as each load uses an 8K char buffer. Issue: SPR-15509
1 parent ed4bd43 commit 07ef7a9

File tree

1 file changed

+31
-10
lines changed

1 file changed

+31
-10
lines changed

spring-core/src/main/java/org/springframework/core/io/support/SpringFactoriesLoader.java

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 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.
@@ -20,17 +20,21 @@
2020
import java.net.URL;
2121
import java.util.ArrayList;
2222
import java.util.Arrays;
23+
import java.util.Collections;
2324
import java.util.Enumeration;
2425
import java.util.List;
26+
import java.util.Map;
2527
import java.util.Properties;
2628

2729
import org.apache.commons.logging.Log;
2830
import org.apache.commons.logging.LogFactory;
29-
3031
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
3132
import org.springframework.core.io.UrlResource;
3233
import org.springframework.util.Assert;
3334
import org.springframework.util.ClassUtils;
35+
import org.springframework.util.ConcurrentReferenceHashMap;
36+
import org.springframework.util.LinkedMultiValueMap;
37+
import org.springframework.util.MultiValueMap;
3438
import org.springframework.util.ReflectionUtils;
3539
import org.springframework.util.StringUtils;
3640

@@ -58,6 +62,8 @@ public abstract class SpringFactoriesLoader {
5862

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

65+
private static Map<ClassLoader, MultiValueMap<String, String>> cache = new ConcurrentReferenceHashMap<>();
66+
6167
/**
6268
* The location to look for factories.
6369
* <p>Can be present in multiple JAR files.
@@ -107,21 +113,36 @@ public static <T> List<T> loadFactories(Class<T> factoryClass, ClassLoader class
107113
*/
108114
public static List<String> loadFactoryNames(Class<?> factoryClass, ClassLoader classLoader) {
109115
String factoryClassName = factoryClass.getName();
116+
return loadSpringFactories(classLoader).getOrDefault(factoryClassName,
117+
Collections.emptyList());
118+
}
119+
120+
private static Map<String, List<String>> loadSpringFactories(
121+
ClassLoader classLoader) {
122+
MultiValueMap<String, String> result = cache.get(classLoader);
123+
if (result != null)
124+
return result;
110125
try {
111-
Enumeration<URL> urls = (classLoader != null ? classLoader.getResources(FACTORIES_RESOURCE_LOCATION) :
112-
ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION));
113-
List<String> result = new ArrayList<>();
126+
Enumeration<URL> urls = (classLoader != null
127+
? classLoader.getResources(FACTORIES_RESOURCE_LOCATION)
128+
: ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION));
129+
result = new LinkedMultiValueMap<>();
114130
while (urls.hasMoreElements()) {
115131
URL url = urls.nextElement();
116-
Properties properties = PropertiesLoaderUtils.loadProperties(new UrlResource(url));
117-
String factoryClassNames = properties.getProperty(factoryClassName);
118-
result.addAll(Arrays.asList(StringUtils.commaDelimitedListToStringArray(factoryClassNames)));
132+
UrlResource resource = new UrlResource(url);
133+
Properties properties = PropertiesLoaderUtils.loadProperties(resource);
134+
for (Map.Entry<?, ?> entry : properties.entrySet()) {
135+
List<String> factoryClassNames = Arrays.asList(
136+
StringUtils.commaDelimitedListToStringArray((String) entry.getValue()));
137+
result.addAll((String) entry.getKey(), factoryClassNames);
138+
}
119139
}
140+
cache.put(classLoader, result);
120141
return result;
121142
}
122143
catch (IOException ex) {
123-
throw new IllegalArgumentException("Unable to load [" + factoryClass.getName() +
124-
"] factories from location [" + FACTORIES_RESOURCE_LOCATION + "]", ex);
144+
throw new IllegalArgumentException("Unable to load factories from location ["
145+
+ FACTORIES_RESOURCE_LOCATION + "]", ex);
125146
}
126147
}
127148

0 commit comments

Comments
 (0)