Skip to content

Commit ac8d1df

Browse files
committed
Enforce limit for storing suppressed exceptions
Closes gh-24902
1 parent f023df7 commit ac8d1df

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultSingletonBeanRegistry.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 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.
@@ -70,6 +70,10 @@
7070
*/
7171
public class DefaultSingletonBeanRegistry extends SimpleAliasRegistry implements SingletonBeanRegistry {
7272

73+
/** Maximum number of suppressed exceptions to preserve. */
74+
private static final int SUPPRESSED_EXCEPTIONS_LIMIT = 100;
75+
76+
7377
/** Cache of singleton objects: bean name to bean instance. */
7478
private final Map<String, Object> singletonObjects = new ConcurrentHashMap<>(256);
7579

@@ -90,7 +94,7 @@ public class DefaultSingletonBeanRegistry extends SimpleAliasRegistry implements
9094
private final Set<String> inCreationCheckExclusions =
9195
Collections.newSetFromMap(new ConcurrentHashMap<>(16));
9296

93-
/** List of suppressed Exceptions, available for associating related causes. */
97+
/** Collection of suppressed Exceptions, available for associating related causes. */
9498
@Nullable
9599
private Set<Exception> suppressedExceptions;
96100

@@ -253,13 +257,17 @@ public Object getSingleton(String beanName, ObjectFactory<?> singletonFactory) {
253257
}
254258

255259
/**
256-
* Register an Exception that happened to get suppressed during the creation of a
260+
* Register an exception that happened to get suppressed during the creation of a
257261
* singleton bean instance, e.g. a temporary circular reference resolution problem.
262+
* <p>The default implementation preserves any given exception in this registry's
263+
* collection of suppressed exceptions, up to a limit of 100 exceptions, adding
264+
* them as related causes to an eventual top-level {@link BeanCreationException}.
258265
* @param ex the Exception to register
266+
* @see BeanCreationException#getRelatedCauses()
259267
*/
260268
protected void onSuppressedException(Exception ex) {
261269
synchronized (this.singletonObjects) {
262-
if (this.suppressedExceptions != null) {
270+
if (this.suppressedExceptions != null && this.suppressedExceptions.size() < SUPPRESSED_EXCEPTIONS_LIMIT) {
263271
this.suppressedExceptions.add(ex);
264272
}
265273
}

0 commit comments

Comments
 (0)