BeanFactoryUtils caches transformedBeanName results for factory beans

Issue: SPR-17151
This commit is contained in:
Juergen Hoeller
2018-08-09 16:06:05 +02:00
parent 6b6384a09e
commit eddbf13d5d
2 changed files with 39 additions and 21 deletions

View File

@@ -22,6 +22,7 @@ import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.springframework.beans.BeansException;
import org.springframework.core.ResolvableType;
@@ -51,6 +52,13 @@ public abstract class BeanFactoryUtils {
*/
public static final String GENERATED_BEAN_NAME_SEPARATOR = "#";
/**
* Cache from name with factory bean prefix to stripped name without dereference.
* @since 5.1
* @see BeanFactory#FACTORY_BEAN_PREFIX
*/
private static final Map<String, String> transformedBeanNameCache = new ConcurrentHashMap<>();
/**
* Return whether the given name is a factory dereference
@@ -72,11 +80,16 @@ public abstract class BeanFactoryUtils {
*/
public static String transformedBeanName(String name) {
Assert.notNull(name, "'name' must not be null");
String beanName = name;
while (beanName.startsWith(BeanFactory.FACTORY_BEAN_PREFIX)) {
beanName = beanName.substring(BeanFactory.FACTORY_BEAN_PREFIX.length());
if (!name.startsWith(BeanFactory.FACTORY_BEAN_PREFIX)) {
return name;
}
return beanName;
return transformedBeanNameCache.computeIfAbsent(name, beanName -> {
do {
beanName = beanName.substring(BeanFactory.FACTORY_BEAN_PREFIX.length());
}
while (beanName.startsWith(BeanFactory.FACTORY_BEAN_PREFIX));
return beanName;
});
}
/**