From 4675bc4e0ca0176b9f1a774e6b030ffe6d861c2c Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Sat, 26 Oct 2013 14:03:11 +0200 Subject: [PATCH] Cache InjectionMetadata per bean name instead of per Class, if possible Issue: SPR-11027 --- .../annotation/AutowiredAnnotationBeanPostProcessor.java | 9 ++++++--- .../annotation/CommonAnnotationBeanPostProcessor.java | 8 +++++--- .../support/PersistenceAnnotationBeanPostProcessor.java | 9 ++++++--- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java b/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java index 5b18d6caeda..5b605b58d64 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java @@ -59,6 +59,7 @@ import org.springframework.core.annotation.AnnotationAttributes; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; import org.springframework.util.ReflectionUtils; +import org.springframework.util.StringUtils; /** * {@link org.springframework.beans.factory.config.BeanPostProcessor} implementation @@ -313,13 +314,15 @@ public class AutowiredAnnotationBeanPostProcessor extends InstantiationAwareBean private InjectionMetadata findAutowiringMetadata(String beanName, Class clazz) { // Quick check on the concurrent map first, with minimal locking. - InjectionMetadata metadata = this.injectionMetadataCache.get(beanName); + // Fall back to class name as cache key, for backwards compatibility with custom callers. + String cacheKey = (StringUtils.hasLength(beanName) ? beanName : clazz.getName()); + InjectionMetadata metadata = this.injectionMetadataCache.get(cacheKey); if (metadata == null) { synchronized (this.injectionMetadataCache) { - metadata = this.injectionMetadataCache.get(beanName); + metadata = this.injectionMetadataCache.get(cacheKey); if (metadata == null) { metadata = buildAutowiringMetadata(clazz); - this.injectionMetadataCache.put(beanName, metadata); + this.injectionMetadataCache.put(cacheKey, metadata); } } } diff --git a/spring-context/src/main/java/org/springframework/context/annotation/CommonAnnotationBeanPostProcessor.java b/spring-context/src/main/java/org/springframework/context/annotation/CommonAnnotationBeanPostProcessor.java index 5e61a4ec751..c68cc5f751d 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/CommonAnnotationBeanPostProcessor.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/CommonAnnotationBeanPostProcessor.java @@ -312,10 +312,12 @@ public class CommonAnnotationBeanPostProcessor extends InitDestroyAnnotationBean private InjectionMetadata findResourceMetadata(String beanName, final Class clazz) { // Quick check on the concurrent map first, with minimal locking. - InjectionMetadata metadata = this.injectionMetadataCache.get(beanName); + // Fall back to class name as cache key, for backwards compatibility with custom callers. + String cacheKey = (StringUtils.hasLength(beanName) ? beanName : clazz.getName()); + InjectionMetadata metadata = this.injectionMetadataCache.get(cacheKey); if (metadata == null) { synchronized (this.injectionMetadataCache) { - metadata = this.injectionMetadataCache.get(beanName); + metadata = this.injectionMetadataCache.get(cacheKey); if (metadata == null) { LinkedList elements = new LinkedList(); Class targetClass = clazz; @@ -389,7 +391,7 @@ public class CommonAnnotationBeanPostProcessor extends InitDestroyAnnotationBean while (targetClass != null && targetClass != Object.class); metadata = new InjectionMetadata(clazz, elements); - this.injectionMetadataCache.put(beanName, metadata); + this.injectionMetadataCache.put(cacheKey, metadata); } } } diff --git a/spring-orm/src/main/java/org/springframework/orm/jpa/support/PersistenceAnnotationBeanPostProcessor.java b/spring-orm/src/main/java/org/springframework/orm/jpa/support/PersistenceAnnotationBeanPostProcessor.java index d7814b08eb3..ef3e4af4d56 100644 --- a/spring-orm/src/main/java/org/springframework/orm/jpa/support/PersistenceAnnotationBeanPostProcessor.java +++ b/spring-orm/src/main/java/org/springframework/orm/jpa/support/PersistenceAnnotationBeanPostProcessor.java @@ -62,6 +62,7 @@ import org.springframework.orm.jpa.SharedEntityManagerCreator; import org.springframework.util.ClassUtils; import org.springframework.util.ObjectUtils; import org.springframework.util.ReflectionUtils; +import org.springframework.util.StringUtils; /** * BeanPostProcessor that processes {@link javax.persistence.PersistenceUnit} @@ -376,10 +377,12 @@ public class PersistenceAnnotationBeanPostProcessor private InjectionMetadata findPersistenceMetadata(String beanName, final Class clazz) { // Quick check on the concurrent map first, with minimal locking. - InjectionMetadata metadata = this.injectionMetadataCache.get(beanName); + // Fall back to class name as cache key, for backwards compatibility with custom callers. + String cacheKey = (StringUtils.hasLength(beanName) ? beanName : clazz.getName()); + InjectionMetadata metadata = this.injectionMetadataCache.get(cacheKey); if (metadata == null) { synchronized (this.injectionMetadataCache) { - metadata = this.injectionMetadataCache.get(beanName); + metadata = this.injectionMetadataCache.get(cacheKey); if (metadata == null) { LinkedList elements = new LinkedList(); Class targetClass = clazz; @@ -417,7 +420,7 @@ public class PersistenceAnnotationBeanPostProcessor while (targetClass != null && targetClass != Object.class); metadata = new InjectionMetadata(clazz, elements); - this.injectionMetadataCache.put(beanName, metadata); + this.injectionMetadataCache.put(cacheKey, metadata); } } }