Skip to content

Commit 4675bc4

Browse files
committed
Cache InjectionMetadata per bean name instead of per Class, if possible
Issue: SPR-11027
1 parent 14b9931 commit 4675bc4

File tree

3 files changed

+17
-9
lines changed

3 files changed

+17
-9
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
import org.springframework.util.Assert;
6060
import org.springframework.util.ClassUtils;
6161
import org.springframework.util.ReflectionUtils;
62+
import org.springframework.util.StringUtils;
6263

6364
/**
6465
* {@link org.springframework.beans.factory.config.BeanPostProcessor} implementation
@@ -313,13 +314,15 @@ public void processInjection(Object bean) throws BeansException {
313314

314315
private InjectionMetadata findAutowiringMetadata(String beanName, Class<?> clazz) {
315316
// Quick check on the concurrent map first, with minimal locking.
316-
InjectionMetadata metadata = this.injectionMetadataCache.get(beanName);
317+
// Fall back to class name as cache key, for backwards compatibility with custom callers.
318+
String cacheKey = (StringUtils.hasLength(beanName) ? beanName : clazz.getName());
319+
InjectionMetadata metadata = this.injectionMetadataCache.get(cacheKey);
317320
if (metadata == null) {
318321
synchronized (this.injectionMetadataCache) {
319-
metadata = this.injectionMetadataCache.get(beanName);
322+
metadata = this.injectionMetadataCache.get(cacheKey);
320323
if (metadata == null) {
321324
metadata = buildAutowiringMetadata(clazz);
322-
this.injectionMetadataCache.put(beanName, metadata);
325+
this.injectionMetadataCache.put(cacheKey, metadata);
323326
}
324327
}
325328
}

spring-context/src/main/java/org/springframework/context/annotation/CommonAnnotationBeanPostProcessor.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -312,10 +312,12 @@ public PropertyValues postProcessPropertyValues(
312312

313313
private InjectionMetadata findResourceMetadata(String beanName, final Class<?> clazz) {
314314
// Quick check on the concurrent map first, with minimal locking.
315-
InjectionMetadata metadata = this.injectionMetadataCache.get(beanName);
315+
// Fall back to class name as cache key, for backwards compatibility with custom callers.
316+
String cacheKey = (StringUtils.hasLength(beanName) ? beanName : clazz.getName());
317+
InjectionMetadata metadata = this.injectionMetadataCache.get(cacheKey);
316318
if (metadata == null) {
317319
synchronized (this.injectionMetadataCache) {
318-
metadata = this.injectionMetadataCache.get(beanName);
320+
metadata = this.injectionMetadataCache.get(cacheKey);
319321
if (metadata == null) {
320322
LinkedList<InjectionMetadata.InjectedElement> elements = new LinkedList<InjectionMetadata.InjectedElement>();
321323
Class<?> targetClass = clazz;
@@ -389,7 +391,7 @@ else if (method.isAnnotationPresent(Resource.class)) {
389391
while (targetClass != null && targetClass != Object.class);
390392

391393
metadata = new InjectionMetadata(clazz, elements);
392-
this.injectionMetadataCache.put(beanName, metadata);
394+
this.injectionMetadataCache.put(cacheKey, metadata);
393395
}
394396
}
395397
}

spring-orm/src/main/java/org/springframework/orm/jpa/support/PersistenceAnnotationBeanPostProcessor.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
import org.springframework.util.ClassUtils;
6363
import org.springframework.util.ObjectUtils;
6464
import org.springframework.util.ReflectionUtils;
65+
import org.springframework.util.StringUtils;
6566

6667
/**
6768
* BeanPostProcessor that processes {@link javax.persistence.PersistenceUnit}
@@ -376,10 +377,12 @@ public void postProcessBeforeDestruction(Object bean, String beanName) throws Be
376377

377378
private InjectionMetadata findPersistenceMetadata(String beanName, final Class<?> clazz) {
378379
// Quick check on the concurrent map first, with minimal locking.
379-
InjectionMetadata metadata = this.injectionMetadataCache.get(beanName);
380+
// Fall back to class name as cache key, for backwards compatibility with custom callers.
381+
String cacheKey = (StringUtils.hasLength(beanName) ? beanName : clazz.getName());
382+
InjectionMetadata metadata = this.injectionMetadataCache.get(cacheKey);
380383
if (metadata == null) {
381384
synchronized (this.injectionMetadataCache) {
382-
metadata = this.injectionMetadataCache.get(beanName);
385+
metadata = this.injectionMetadataCache.get(cacheKey);
383386
if (metadata == null) {
384387
LinkedList<InjectionMetadata.InjectedElement> elements = new LinkedList<InjectionMetadata.InjectedElement>();
385388
Class<?> targetClass = clazz;
@@ -417,7 +420,7 @@ private InjectionMetadata findPersistenceMetadata(String beanName, final Class<?
417420
while (targetClass != null && targetClass != Object.class);
418421

419422
metadata = new InjectionMetadata(clazz, elements);
420-
this.injectionMetadataCache.put(beanName, metadata);
423+
this.injectionMetadataCache.put(cacheKey, metadata);
421424
}
422425
}
423426
}

0 commit comments

Comments
 (0)