Skip to content

Commit b146074

Browse files
committed
InjectionMetadata caching per bean name needs to refresh when bean class changes
Issue: SPR-11246
1 parent 2faf008 commit b146074

File tree

4 files changed

+11
-6
lines changed

4 files changed

+11
-6
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,10 +317,10 @@ private InjectionMetadata findAutowiringMetadata(String beanName, Class<?> clazz
317317
// Fall back to class name as cache key, for backwards compatibility with custom callers.
318318
String cacheKey = (StringUtils.hasLength(beanName) ? beanName : clazz.getName());
319319
InjectionMetadata metadata = this.injectionMetadataCache.get(cacheKey);
320-
if (metadata == null) {
320+
if (InjectionMetadata.needsRefresh(metadata, clazz)) {
321321
synchronized (this.injectionMetadataCache) {
322322
metadata = this.injectionMetadataCache.get(cacheKey);
323-
if (metadata == null) {
323+
if (InjectionMetadata.needsRefresh(metadata, clazz)) {
324324
metadata = buildAutowiringMetadata(clazz);
325325
this.injectionMetadataCache.put(cacheKey, metadata);
326326
}

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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,10 +315,10 @@ private InjectionMetadata findResourceMetadata(String beanName, final Class<?> c
315315
// Fall back to class name as cache key, for backwards compatibility with custom callers.
316316
String cacheKey = (StringUtils.hasLength(beanName) ? beanName : clazz.getName());
317317
InjectionMetadata metadata = this.injectionMetadataCache.get(cacheKey);
318-
if (metadata == null) {
318+
if (InjectionMetadata.needsRefresh(metadata, clazz)) {
319319
synchronized (this.injectionMetadataCache) {
320320
metadata = this.injectionMetadataCache.get(cacheKey);
321-
if (metadata == null) {
321+
if (InjectionMetadata.needsRefresh(metadata, clazz)) {
322322
LinkedList<InjectionMetadata.InjectedElement> elements = new LinkedList<InjectionMetadata.InjectedElement>();
323323
Class<?> targetClass = clazz;
324324

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
@@ -380,10 +380,10 @@ private InjectionMetadata findPersistenceMetadata(String beanName, final Class<?
380380
// Fall back to class name as cache key, for backwards compatibility with custom callers.
381381
String cacheKey = (StringUtils.hasLength(beanName) ? beanName : clazz.getName());
382382
InjectionMetadata metadata = this.injectionMetadataCache.get(cacheKey);
383-
if (metadata == null) {
383+
if (InjectionMetadata.needsRefresh(metadata, clazz)) {
384384
synchronized (this.injectionMetadataCache) {
385385
metadata = this.injectionMetadataCache.get(cacheKey);
386-
if (metadata == null) {
386+
if (InjectionMetadata.needsRefresh(metadata, clazz)) {
387387
LinkedList<InjectionMetadata.InjectedElement> elements = new LinkedList<InjectionMetadata.InjectedElement>();
388388
Class<?> targetClass = clazz;
389389

0 commit comments

Comments
 (0)