Skip to content

Commit 78fce80

Browse files
committed
AnnotationUtils.clearCache() includes all annotation caches
Closes gh-31170
1 parent 268043e commit 78fce80

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
@@ -1081,6 +1081,9 @@ protected void doClose() {
10811081
// Let subclasses do some final clean-up if they wish...
10821082
onClose();
10831083

1084+
// Reset common introspection caches to avoid class reference leaks.
1085+
resetCommonCaches();
1086+
10841087
// Reset local application listeners to pre-refresh state.
10851088
if (this.earlyApplicationListeners != null) {
10861089
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
@@ -1326,6 +1326,9 @@ public static boolean isSynthesizedAnnotation(@Nullable Annotation annotation) {
13261326
public static void clearCache() {
13271327
AnnotationTypeMappings.clearCache();
13281328
AnnotationsScanner.clearCache();
1329+
AttributeMethods.cache.clear();
1330+
RepeatableContainers.cache.clear();
1331+
OrderUtils.orderCache.clear();
13291332
}
13301333

13311334

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-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.
@@ -41,7 +41,7 @@ public abstract class OrderUtils {
4141
private static final String JAVAX_PRIORITY_ANNOTATION = "jakarta.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

@@ -141,8 +143,6 @@ public static RepeatableContainers none() {
141143
*/
142144
private static class StandardRepeatableContainers extends RepeatableContainers {
143145

144-
private static final Map<Class<? extends Annotation>, Object> cache = new ConcurrentReferenceHashMap<>();
145-
146146
private static final Object NONE = new Object();
147147

148148
private static final StandardRepeatableContainers INSTANCE = new StandardRepeatableContainers();

0 commit comments

Comments
 (0)