Cache InjectionMetadata per bean name instead of per Class, if possible
Issue: SPR-11027
This commit is contained in:
parent
14b9931ee3
commit
4675bc4e0c
|
|
@ -59,6 +59,7 @@ import org.springframework.core.annotation.AnnotationAttributes;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
import org.springframework.util.ClassUtils;
|
import org.springframework.util.ClassUtils;
|
||||||
import org.springframework.util.ReflectionUtils;
|
import org.springframework.util.ReflectionUtils;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@link org.springframework.beans.factory.config.BeanPostProcessor} implementation
|
* {@link org.springframework.beans.factory.config.BeanPostProcessor} implementation
|
||||||
|
|
@ -313,13 +314,15 @@ public class AutowiredAnnotationBeanPostProcessor extends InstantiationAwareBean
|
||||||
|
|
||||||
private InjectionMetadata findAutowiringMetadata(String beanName, Class<?> clazz) {
|
private InjectionMetadata findAutowiringMetadata(String beanName, Class<?> clazz) {
|
||||||
// Quick check on the concurrent map first, with minimal locking.
|
// 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) {
|
if (metadata == null) {
|
||||||
synchronized (this.injectionMetadataCache) {
|
synchronized (this.injectionMetadataCache) {
|
||||||
metadata = this.injectionMetadataCache.get(beanName);
|
metadata = this.injectionMetadataCache.get(cacheKey);
|
||||||
if (metadata == null) {
|
if (metadata == null) {
|
||||||
metadata = buildAutowiringMetadata(clazz);
|
metadata = buildAutowiringMetadata(clazz);
|
||||||
this.injectionMetadataCache.put(beanName, metadata);
|
this.injectionMetadataCache.put(cacheKey, metadata);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -312,10 +312,12 @@ public class CommonAnnotationBeanPostProcessor extends InitDestroyAnnotationBean
|
||||||
|
|
||||||
private InjectionMetadata findResourceMetadata(String beanName, final Class<?> clazz) {
|
private InjectionMetadata findResourceMetadata(String beanName, final Class<?> clazz) {
|
||||||
// Quick check on the concurrent map first, with minimal locking.
|
// 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) {
|
if (metadata == null) {
|
||||||
synchronized (this.injectionMetadataCache) {
|
synchronized (this.injectionMetadataCache) {
|
||||||
metadata = this.injectionMetadataCache.get(beanName);
|
metadata = this.injectionMetadataCache.get(cacheKey);
|
||||||
if (metadata == null) {
|
if (metadata == null) {
|
||||||
LinkedList<InjectionMetadata.InjectedElement> elements = new LinkedList<InjectionMetadata.InjectedElement>();
|
LinkedList<InjectionMetadata.InjectedElement> elements = new LinkedList<InjectionMetadata.InjectedElement>();
|
||||||
Class<?> targetClass = clazz;
|
Class<?> targetClass = clazz;
|
||||||
|
|
@ -389,7 +391,7 @@ public class CommonAnnotationBeanPostProcessor extends InitDestroyAnnotationBean
|
||||||
while (targetClass != null && targetClass != Object.class);
|
while (targetClass != null && targetClass != Object.class);
|
||||||
|
|
||||||
metadata = new InjectionMetadata(clazz, elements);
|
metadata = new InjectionMetadata(clazz, elements);
|
||||||
this.injectionMetadataCache.put(beanName, metadata);
|
this.injectionMetadataCache.put(cacheKey, metadata);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -62,6 +62,7 @@ import org.springframework.orm.jpa.SharedEntityManagerCreator;
|
||||||
import org.springframework.util.ClassUtils;
|
import org.springframework.util.ClassUtils;
|
||||||
import org.springframework.util.ObjectUtils;
|
import org.springframework.util.ObjectUtils;
|
||||||
import org.springframework.util.ReflectionUtils;
|
import org.springframework.util.ReflectionUtils;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* BeanPostProcessor that processes {@link javax.persistence.PersistenceUnit}
|
* BeanPostProcessor that processes {@link javax.persistence.PersistenceUnit}
|
||||||
|
|
@ -376,10 +377,12 @@ public class PersistenceAnnotationBeanPostProcessor
|
||||||
|
|
||||||
private InjectionMetadata findPersistenceMetadata(String beanName, final Class<?> clazz) {
|
private InjectionMetadata findPersistenceMetadata(String beanName, final Class<?> clazz) {
|
||||||
// Quick check on the concurrent map first, with minimal locking.
|
// 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) {
|
if (metadata == null) {
|
||||||
synchronized (this.injectionMetadataCache) {
|
synchronized (this.injectionMetadataCache) {
|
||||||
metadata = this.injectionMetadataCache.get(beanName);
|
metadata = this.injectionMetadataCache.get(cacheKey);
|
||||||
if (metadata == null) {
|
if (metadata == null) {
|
||||||
LinkedList<InjectionMetadata.InjectedElement> elements = new LinkedList<InjectionMetadata.InjectedElement>();
|
LinkedList<InjectionMetadata.InjectedElement> elements = new LinkedList<InjectionMetadata.InjectedElement>();
|
||||||
Class<?> targetClass = clazz;
|
Class<?> targetClass = clazz;
|
||||||
|
|
@ -417,7 +420,7 @@ public class PersistenceAnnotationBeanPostProcessor
|
||||||
while (targetClass != null && targetClass != Object.class);
|
while (targetClass != null && targetClass != Object.class);
|
||||||
|
|
||||||
metadata = new InjectionMetadata(clazz, elements);
|
metadata = new InjectionMetadata(clazz, elements);
|
||||||
this.injectionMetadataCache.put(beanName, metadata);
|
this.injectionMetadataCache.put(cacheKey, metadata);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue