Skip to content

Commit dbd47ff

Browse files
committed
Implement additional micro performance optimizations
See gh-34717
1 parent 381bc4c commit dbd47ff

File tree

6 files changed

+24
-25
lines changed

6 files changed

+24
-25
lines changed

Diff for: spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/BeanNameAutoProxyCreator.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2025 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.
@@ -114,10 +114,10 @@ private boolean isSupportedBeanName(Class<?> beanClass, String beanName) {
114114
boolean isFactoryBean = FactoryBean.class.isAssignableFrom(beanClass);
115115
for (String mappedName : this.beanNames) {
116116
if (isFactoryBean) {
117-
if (!mappedName.startsWith(BeanFactory.FACTORY_BEAN_PREFIX)) {
117+
if (mappedName.isEmpty() || mappedName.charAt(0) != BeanFactory.FACTORY_BEAN_PREFIX_CHAR) {
118118
continue;
119119
}
120-
mappedName = mappedName.substring(BeanFactory.FACTORY_BEAN_PREFIX.length());
120+
mappedName = mappedName.substring(1); // length of '&'
121121
}
122122
if (isMatch(beanName, mappedName)) {
123123
return true;

Diff for: spring-beans/src/main/java/org/springframework/beans/factory/BeanFactory.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2025 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.
@@ -124,9 +124,16 @@ public interface BeanFactory {
124124
* beans <i>created</i> by the FactoryBean. For example, if the bean named
125125
* {@code myJndiObject} is a FactoryBean, getting {@code &myJndiObject}
126126
* will return the factory, not the instance returned by the factory.
127+
* @see #FACTORY_BEAN_PREFIX_CHAR
127128
*/
128129
String FACTORY_BEAN_PREFIX = "&";
129130

131+
/**
132+
* Character variant of {@link #FACTORY_BEAN_PREFIX}.
133+
* @since 6.2.6
134+
*/
135+
char FACTORY_BEAN_PREFIX_CHAR = '&';
136+
130137

131138
/**
132139
* Return an instance, which may be shared or independent, of the specified bean.

Diff for: spring-beans/src/main/java/org/springframework/beans/factory/BeanFactoryUtils.java

+3-11
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,6 @@ public abstract class BeanFactoryUtils {
6464
private static final Map<String, String> transformedBeanNameCache = new ConcurrentHashMap<>();
6565

6666

67-
/**
68-
* Used to dereference a {@link FactoryBean} instance and distinguish it from
69-
* beans <i>created</i> by the FactoryBean. For example, if the bean named
70-
* {@code myJndiObject} is a FactoryBean, getting {@code &myJndiObject}
71-
* will return the factory, not the instance returned by the factory.
72-
*/
73-
private static final char FACTORY_BEAN_PREFIX = BeanFactory.FACTORY_BEAN_PREFIX.charAt(0);
74-
7567
/**
7668
* Return whether the given name is a factory dereference
7769
* (beginning with the factory dereference prefix).
@@ -92,14 +84,14 @@ public static boolean isFactoryDereference(@Nullable String name) {
9284
*/
9385
public static String transformedBeanName(String name) {
9486
Assert.notNull(name, "'name' must not be null");
95-
if (!isFactoryDereference(name)) {
87+
if (name.isEmpty() || name.charAt(0) != BeanFactory.FACTORY_BEAN_PREFIX_CHAR) {
9688
return name;
9789
}
9890
return transformedBeanNameCache.computeIfAbsent(name, beanName -> {
9991
do {
100-
beanName = beanName.substring(BeanFactory.FACTORY_BEAN_PREFIX.length());
92+
beanName = beanName.substring(1); // length of '&'
10193
}
102-
while (isFactoryDereference(beanName));
94+
while (beanName.charAt(0) == BeanFactory.FACTORY_BEAN_PREFIX_CHAR);
10395
return beanName;
10496
});
10597
}

Diff for: spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -770,16 +770,16 @@ else if (BeanFactoryUtils.isFactoryDereference(name)) {
770770
public String[] getAliases(String name) {
771771
String beanName = transformedBeanName(name);
772772
List<String> aliases = new ArrayList<>();
773-
boolean factoryPrefix = name.startsWith(FACTORY_BEAN_PREFIX);
773+
boolean hasFactoryPrefix = (!name.isEmpty() && name.charAt(0) == BeanFactory.FACTORY_BEAN_PREFIX_CHAR);
774774
String fullBeanName = beanName;
775-
if (factoryPrefix) {
775+
if (hasFactoryPrefix) {
776776
fullBeanName = FACTORY_BEAN_PREFIX + beanName;
777777
}
778778
if (!fullBeanName.equals(name)) {
779779
aliases.add(fullBeanName);
780780
}
781781
String[] retrievedAliases = super.getAliases(beanName);
782-
String prefix = (factoryPrefix ? FACTORY_BEAN_PREFIX : "");
782+
String prefix = (hasFactoryPrefix ? FACTORY_BEAN_PREFIX : "");
783783
for (String retrievedAlias : retrievedAliases) {
784784
String alias = prefix + retrievedAlias;
785785
if (!alias.equals(name)) {
@@ -1292,7 +1292,7 @@ protected String transformedBeanName(String name) {
12921292
*/
12931293
protected String originalBeanName(String name) {
12941294
String beanName = transformedBeanName(name);
1295-
if (name.startsWith(FACTORY_BEAN_PREFIX)) {
1295+
if (!name.isEmpty() && name.charAt(0) == BeanFactory.FACTORY_BEAN_PREFIX_CHAR) {
12961296
beanName = FACTORY_BEAN_PREFIX + beanName;
12971297
}
12981298
return beanName;

Diff for: spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassEnhancer.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -364,9 +364,9 @@ public Object intercept(Object enhancedConfigInstance, Method beanMethod, Object
364364
// proxy that intercepts calls to getObject() and returns any cached bean instance.
365365
// This ensures that the semantics of calling a FactoryBean from within @Bean methods
366366
// is the same as that of referring to a FactoryBean within XML. See SPR-6602.
367-
if (factoryContainsBean(beanFactory, BeanFactory.FACTORY_BEAN_PREFIX + beanName) &&
368-
factoryContainsBean(beanFactory, beanName)) {
369-
Object factoryBean = beanFactory.getBean(BeanFactory.FACTORY_BEAN_PREFIX + beanName);
367+
String factoryBeanName = BeanFactory.FACTORY_BEAN_PREFIX + beanName;
368+
if (factoryContainsBean(beanFactory, factoryBeanName) && factoryContainsBean(beanFactory, beanName)) {
369+
Object factoryBean = beanFactory.getBean(factoryBeanName);
370370
if (factoryBean instanceof ScopedProxyFactoryBean) {
371371
// Scoped proxy factory beans are a special case and should not be further proxied
372372
}

Diff for: spring-context/src/main/java/org/springframework/jmx/export/MBeanExporter.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 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.
@@ -932,8 +932,8 @@ private void autodetect(Map<String, Object> beans, AutodetectCallback callback)
932932
*/
933933
private boolean isExcluded(String beanName) {
934934
return (this.excludedBeans.contains(beanName) ||
935-
(beanName.startsWith(BeanFactory.FACTORY_BEAN_PREFIX) &&
936-
this.excludedBeans.contains(beanName.substring(BeanFactory.FACTORY_BEAN_PREFIX.length()))));
935+
(!beanName.isEmpty() && (beanName.charAt(0) == BeanFactory.FACTORY_BEAN_PREFIX_CHAR) &&
936+
this.excludedBeans.contains(beanName.substring(1)))); // length of '&'
937937
}
938938

939939
/**

0 commit comments

Comments
 (0)