Skip to content

Commit 7b711c4

Browse files
committed
AbstractAutoProxyCreator uses prefixed bean name as cache key
Issue: SPR-13655
1 parent 90c9d96 commit 7b711c4

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/AbstractAutoProxyCreator.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,11 @@
4242
import org.springframework.beans.PropertyValues;
4343
import org.springframework.beans.factory.BeanFactory;
4444
import org.springframework.beans.factory.BeanFactoryAware;
45+
import org.springframework.beans.factory.FactoryBean;
4546
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
4647
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
4748
import org.springframework.beans.factory.config.SmartInstantiationAwareBeanPostProcessor;
49+
import org.springframework.util.StringUtils;
4850

4951
/**
5052
* {@link org.springframework.beans.factory.config.BeanPostProcessor} implementation
@@ -125,8 +127,6 @@ public abstract class AbstractAutoProxyCreator extends ProxyProcessorSupport
125127

126128
private BeanFactory beanFactory;
127129

128-
private final Map<Object, Boolean> advisedBeans = new ConcurrentHashMap<Object, Boolean>(64);
129-
130130
private final Set<String> targetSourcedBeans =
131131
Collections.newSetFromMap(new ConcurrentHashMap<String, Boolean>(16));
132132

@@ -135,6 +135,8 @@ public abstract class AbstractAutoProxyCreator extends ProxyProcessorSupport
135135

136136
private final Map<Object, Class<?>> proxyTypes = new ConcurrentHashMap<Object, Class<?>>(16);
137137

138+
private final Map<Object, Boolean> advisedBeans = new ConcurrentHashMap<Object, Boolean>(256);
139+
138140

139141
/**
140142
* Set whether or not the proxy should be frozen, preventing advice
@@ -214,6 +216,9 @@ protected BeanFactory getBeanFactory() {
214216

215217
@Override
216218
public Class<?> predictBeanType(Class<?> beanClass, String beanName) {
219+
if (this.proxyTypes.isEmpty()) {
220+
return null;
221+
}
217222
Object cacheKey = getCacheKey(beanClass, beanName);
218223
return this.proxyTypes.get(cacheKey);
219224
}
@@ -299,12 +304,23 @@ public Object postProcessAfterInitialization(Object bean, String beanName) throw
299304

300305
/**
301306
* Build a cache key for the given bean class and bean name.
307+
* <p>Note: As of 4.2.3, this implementation does not return a concatenated
308+
* class/name String anymore but rather the most efficient cache key possible:
309+
* a plain bean name, prepended with {@link BeanFactory#FACTORY_BEAN_PREFIX}
310+
* in case of a {@code FactoryBean}; or if no bean name specified, then the
311+
* given bean {@code Class} as-is.
302312
* @param beanClass the bean class
303313
* @param beanName the bean name
304314
* @return the cache key for the given class and name
305315
*/
306316
protected Object getCacheKey(Class<?> beanClass, String beanName) {
307-
return beanClass.getName() + "_" + beanName;
317+
if (StringUtils.hasLength(beanName)) {
318+
return (FactoryBean.class.isAssignableFrom(beanClass) ?
319+
BeanFactory.FACTORY_BEAN_PREFIX + beanName : beanName);
320+
}
321+
else {
322+
return beanClass;
323+
}
308324
}
309325

310326
/**
@@ -420,7 +436,7 @@ protected TargetSource getCustomTargetSource(Class<?> beanClass, String beanName
420436
protected Object createProxy(
421437
Class<?> beanClass, String beanName, Object[] specificInterceptors, TargetSource targetSource) {
422438

423-
if (beanName != null && this.beanFactory instanceof ConfigurableListableBeanFactory) {
439+
if (this.beanFactory instanceof ConfigurableListableBeanFactory) {
424440
AutoProxyUtils.exposeTargetClass((ConfigurableListableBeanFactory) this.beanFactory, beanName, beanClass);
425441
}
426442

0 commit comments

Comments
 (0)