Browse Source

InjectionMetadata caching per bean name needs to refresh when bean class changes

Issue: SPR-11246
pull/439/head
Juergen Hoeller 12 years ago
parent
commit
08aa22ff1f
  1. 8
      spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java
  2. 5
      spring-beans/src/main/java/org/springframework/beans/factory/annotation/InjectionMetadata.java
  3. 6
      spring-context/src/main/java/org/springframework/context/annotation/CommonAnnotationBeanPostProcessor.java
  4. 4
      spring-orm/src/main/java/org/springframework/orm/jpa/support/PersistenceAnnotationBeanPostProcessor.java

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

@ -265,10 +265,10 @@ public class AutowiredAnnotationBeanPostProcessor extends InstantiationAwareBean @@ -265,10 +265,10 @@ public class AutowiredAnnotationBeanPostProcessor extends InstantiationAwareBean
if (requiredConstructor == null && defaultConstructor != null) {
candidates.add(defaultConstructor);
}
candidateConstructors = candidates.toArray(new Constructor[candidates.size()]);
candidateConstructors = candidates.toArray(new Constructor<?>[candidates.size()]);
}
else {
candidateConstructors = new Constructor[0];
candidateConstructors = new Constructor<?>[0];
}
this.candidateConstructorsCache.put(beanClass, candidateConstructors);
}
@ -314,10 +314,10 @@ public class AutowiredAnnotationBeanPostProcessor extends InstantiationAwareBean @@ -314,10 +314,10 @@ public class AutowiredAnnotationBeanPostProcessor extends InstantiationAwareBean
// 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 (InjectionMetadata.needsRefresh(metadata, clazz)) {
synchronized (this.injectionMetadataCache) {
metadata = this.injectionMetadataCache.get(cacheKey);
if (metadata == null) {
if (InjectionMetadata.needsRefresh(metadata, clazz)) {
metadata = buildAutowiringMetadata(clazz);
this.injectionMetadataCache.put(cacheKey, metadata);
}

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

@ -90,6 +90,11 @@ public class InjectionMetadata { @@ -90,6 +90,11 @@ public class InjectionMetadata {
}
public static boolean needsRefresh(InjectionMetadata metadata, Class<?> clazz) {
return (metadata == null || !metadata.targetClass.equals(clazz));
}
public static abstract class InjectedElement {
protected final Member member;

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

@ -314,10 +314,10 @@ public class CommonAnnotationBeanPostProcessor extends InitDestroyAnnotationBean @@ -314,10 +314,10 @@ public class CommonAnnotationBeanPostProcessor extends InitDestroyAnnotationBean
// 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 (InjectionMetadata.needsRefresh(metadata, clazz)) {
synchronized (this.injectionMetadataCache) {
metadata = this.injectionMetadataCache.get(cacheKey);
if (metadata == null) {
if (InjectionMetadata.needsRefresh(metadata, clazz)) {
LinkedList<InjectionMetadata.InjectedElement> elements = new LinkedList<InjectionMetadata.InjectedElement>();
Class<?> targetClass = clazz;
@ -606,7 +606,7 @@ public class CommonAnnotationBeanPostProcessor extends InitDestroyAnnotationBean @@ -606,7 +606,7 @@ public class CommonAnnotationBeanPostProcessor extends InitDestroyAnnotationBean
}
if (StringUtils.hasLength(this.wsdlLocation)) {
try {
Constructor<?> ctor = this.lookupType.getConstructor(new Class[] {URL.class, QName.class});
Constructor<?> ctor = this.lookupType.getConstructor(new Class<?>[] {URL.class, QName.class});
WebServiceClient clientAnn = this.lookupType.getAnnotation(WebServiceClient.class);
if (clientAnn == null) {
throw new IllegalStateException("JAX-WS Service class [" + this.lookupType.getName() +

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

@ -365,10 +365,10 @@ public class PersistenceAnnotationBeanPostProcessor @@ -365,10 +365,10 @@ public class PersistenceAnnotationBeanPostProcessor
// 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 (InjectionMetadata.needsRefresh(metadata, clazz)) {
synchronized (this.injectionMetadataCache) {
metadata = this.injectionMetadataCache.get(cacheKey);
if (metadata == null) {
if (InjectionMetadata.needsRefresh(metadata, clazz)) {
LinkedList<InjectionMetadata.InjectedElement> elements = new LinkedList<InjectionMetadata.InjectedElement>();
Class<?> targetClass = clazz;

Loading…
Cancel
Save