Skip to content

Commit 39c225c

Browse files
committed
AnnotationUtils.clearCache() includes all annotation caches
Closes gh-31170 (cherry picked from commit 78fce80)
1 parent 0c3d8d7 commit 39c225c

File tree

5 files changed

+12
-8
lines changed

5 files changed

+12
-8
lines changed

spring-context/src/main/java/org/springframework/context/support/AbstractApplicationContext.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,6 +1083,9 @@ protected void doClose() {
10831083
// Let subclasses do some final clean-up if they wish...
10841084
onClose();
10851085

1086+
// Reset common introspection caches to avoid class reference leaks.
1087+
resetCommonCaches();
1088+
10861089
// Reset local application listeners to pre-refresh state.
10871090
if (this.earlyApplicationListeners != null) {
10881091
this.applicationListeners.clear();

spring-core/src/main/java/org/springframework/core/annotation/AnnotationUtils.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,6 +1321,9 @@ public static boolean isSynthesizedAnnotation(@Nullable Annotation annotation) {
13211321
public static void clearCache() {
13221322
AnnotationTypeMappings.clearCache();
13231323
AnnotationsScanner.clearCache();
1324+
AttributeMethods.cache.clear();
1325+
RepeatableContainers.cache.clear();
1326+
OrderUtils.orderCache.clear();
13241327
}
13251328

13261329

spring-core/src/main/java/org/springframework/core/annotation/AttributeMethods.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -39,9 +39,7 @@ final class AttributeMethods {
3939

4040
static final AttributeMethods NONE = new AttributeMethods(null, new Method[0]);
4141

42-
43-
private static final Map<Class<? extends Annotation>, AttributeMethods> cache =
44-
new ConcurrentReferenceHashMap<>();
42+
static final Map<Class<? extends Annotation>, AttributeMethods> cache = new ConcurrentReferenceHashMap<>();
4543

4644
private static final Comparator<Method> methodComparator = (m1, m2) -> {
4745
if (m1 != null && m2 != null) {

spring-core/src/main/java/org/springframework/core/annotation/OrderUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -41,7 +41,7 @@ public abstract class OrderUtils {
4141
private static final String JAVAX_PRIORITY_ANNOTATION = "javax.annotation.Priority";
4242

4343
/** Cache for @Order value (or NOT_ANNOTATED marker) per Class. */
44-
private static final Map<AnnotatedElement, Object> orderCache = new ConcurrentReferenceHashMap<>(64);
44+
static final Map<AnnotatedElement, Object> orderCache = new ConcurrentReferenceHashMap<>(64);
4545

4646

4747
/**

spring-core/src/main/java/org/springframework/core/annotation/RepeatableContainers.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
*/
4444
public abstract class RepeatableContainers {
4545

46+
static final Map<Class<? extends Annotation>, Object> cache = new ConcurrentReferenceHashMap<>();
47+
4648
@Nullable
4749
private final RepeatableContainers parent;
4850

@@ -137,8 +139,6 @@ public static RepeatableContainers none() {
137139
*/
138140
private static class StandardRepeatableContainers extends RepeatableContainers {
139141

140-
private static final Map<Class<? extends Annotation>, Object> cache = new ConcurrentReferenceHashMap<>();
141-
142142
private static final Object NONE = new Object();
143143

144144
private static StandardRepeatableContainers INSTANCE = new StandardRepeatableContainers();

0 commit comments

Comments
 (0)