Skip to content

Commit 08aa22f

Browse files
committed
InjectionMetadata caching per bean name needs to refresh when bean class changes
Issue: SPR-11246
1 parent a115a7d commit 08aa22f

File tree

4 files changed

+14
-9
lines changed

4 files changed

+14
-9
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -265,10 +265,10 @@ else if (candidate.getParameterTypes().length == 0) {
265265
if (requiredConstructor == null && defaultConstructor != null) {
266266
candidates.add(defaultConstructor);
267267
}
268-
candidateConstructors = candidates.toArray(new Constructor[candidates.size()]);
268+
candidateConstructors = candidates.toArray(new Constructor<?>[candidates.size()]);
269269
}
270270
else {
271-
candidateConstructors = new Constructor[0];
271+
candidateConstructors = new Constructor<?>[0];
272272
}
273273
this.candidateConstructorsCache.put(beanClass, candidateConstructors);
274274
}
@@ -314,10 +314,10 @@ private InjectionMetadata findAutowiringMetadata(String beanName, Class<?> clazz
314314
// Fall back to class name as cache key, for backwards compatibility with custom callers.
315315
String cacheKey = (StringUtils.hasLength(beanName) ? beanName : clazz.getName());
316316
InjectionMetadata metadata = this.injectionMetadataCache.get(cacheKey);
317-
if (metadata == null) {
317+
if (InjectionMetadata.needsRefresh(metadata, clazz)) {
318318
synchronized (this.injectionMetadataCache) {
319319
metadata = this.injectionMetadataCache.get(cacheKey);
320-
if (metadata == null) {
320+
if (InjectionMetadata.needsRefresh(metadata, clazz)) {
321321
metadata = buildAutowiringMetadata(clazz);
322322
this.injectionMetadataCache.put(cacheKey, metadata);
323323
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ public void inject(Object target, String beanName, PropertyValues pvs) throws Th
9090
}
9191

9292

93+
public static boolean needsRefresh(InjectionMetadata metadata, Class<?> clazz) {
94+
return (metadata == null || !metadata.targetClass.equals(clazz));
95+
}
96+
97+
9398
public static abstract class InjectedElement {
9499

95100
protected final Member member;

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -314,10 +314,10 @@ private InjectionMetadata findResourceMetadata(String beanName, final Class<?> c
314314
// Fall back to class name as cache key, for backwards compatibility with custom callers.
315315
String cacheKey = (StringUtils.hasLength(beanName) ? beanName : clazz.getName());
316316
InjectionMetadata metadata = this.injectionMetadataCache.get(cacheKey);
317-
if (metadata == null) {
317+
if (InjectionMetadata.needsRefresh(metadata, clazz)) {
318318
synchronized (this.injectionMetadataCache) {
319319
metadata = this.injectionMetadataCache.get(cacheKey);
320-
if (metadata == null) {
320+
if (InjectionMetadata.needsRefresh(metadata, clazz)) {
321321
LinkedList<InjectionMetadata.InjectedElement> elements = new LinkedList<InjectionMetadata.InjectedElement>();
322322
Class<?> targetClass = clazz;
323323

@@ -606,7 +606,7 @@ protected Object getResourceToInject(Object target, String requestingBeanName) {
606606
}
607607
if (StringUtils.hasLength(this.wsdlLocation)) {
608608
try {
609-
Constructor<?> ctor = this.lookupType.getConstructor(new Class[] {URL.class, QName.class});
609+
Constructor<?> ctor = this.lookupType.getConstructor(new Class<?>[] {URL.class, QName.class});
610610
WebServiceClient clientAnn = this.lookupType.getAnnotation(WebServiceClient.class);
611611
if (clientAnn == null) {
612612
throw new IllegalStateException("JAX-WS Service class [" + this.lookupType.getName() +

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,10 +365,10 @@ private InjectionMetadata findPersistenceMetadata(String beanName, final Class<?
365365
// Fall back to class name as cache key, for backwards compatibility with custom callers.
366366
String cacheKey = (StringUtils.hasLength(beanName) ? beanName : clazz.getName());
367367
InjectionMetadata metadata = this.injectionMetadataCache.get(cacheKey);
368-
if (metadata == null) {
368+
if (InjectionMetadata.needsRefresh(metadata, clazz)) {
369369
synchronized (this.injectionMetadataCache) {
370370
metadata = this.injectionMetadataCache.get(cacheKey);
371-
if (metadata == null) {
371+
if (InjectionMetadata.needsRefresh(metadata, clazz)) {
372372
LinkedList<InjectionMetadata.InjectedElement> elements = new LinkedList<InjectionMetadata.InjectedElement>();
373373
Class<?> targetClass = clazz;
374374

0 commit comments

Comments
 (0)