@@ -135,56 +135,59 @@ public ApplicationContext loadContext(MergedContextConfiguration mergedConfig) {
135
135
mergedConfig = replaceIfNecessary (mergedConfig );
136
136
synchronized (this .contextCache ) {
137
137
ApplicationContext context = this .contextCache .get (mergedConfig );
138
- if (context == null ) {
139
- Integer failureCount = this .contextCache .getFailureCount (mergedConfig );
140
- if (failureCount >= this .failureThreshold ) {
141
- throw new IllegalStateException ("""
142
- ApplicationContext failure threshold (%d) exceeded: \
143
- skipping repeated attempt to load context for %s"""
144
- .formatted (this .failureThreshold , mergedConfig ));
145
- }
146
- try {
147
- if (mergedConfig instanceof AotMergedContextConfiguration aotMergedConfig ) {
148
- context = loadContextInAotMode (aotMergedConfig );
149
- }
150
- else {
151
- context = loadContextInternal (mergedConfig );
138
+ try {
139
+ if (context == null ) {
140
+ Integer failureCount = this .contextCache .getFailureCount (mergedConfig );
141
+ if (failureCount >= this .failureThreshold ) {
142
+ throw new IllegalStateException ("""
143
+ ApplicationContext failure threshold (%d) exceeded: \
144
+ skipping repeated attempt to load context for %s"""
145
+ .formatted (this .failureThreshold , mergedConfig ));
152
146
}
153
- if (logger .isTraceEnabled ()) {
154
- logger .trace ("Storing ApplicationContext [%s] in cache under key %s" .formatted (
155
- System .identityHashCode (context ), mergedConfig ));
147
+ try {
148
+ if (mergedConfig instanceof AotMergedContextConfiguration aotMergedConfig ) {
149
+ context = loadContextInAotMode (aotMergedConfig );
150
+ }
151
+ else {
152
+ context = loadContextInternal (mergedConfig );
153
+ }
154
+ if (logger .isTraceEnabled ()) {
155
+ logger .trace ("Storing ApplicationContext [%s] in cache under key %s" .formatted (
156
+ System .identityHashCode (context ), mergedConfig ));
157
+ }
158
+ this .contextCache .put (mergedConfig , context );
156
159
}
157
- this .contextCache .put (mergedConfig , context );
158
- }
159
- catch (Exception ex ) {
160
- this .contextCache .incrementFailureCount (mergedConfig );
161
- Throwable cause = ex ;
162
- if (ex instanceof ContextLoadException cle ) {
163
- cause = cle .getCause ();
164
- for (ApplicationContextFailureProcessor contextFailureProcessor : this .contextFailureProcessors ) {
165
- try {
166
- contextFailureProcessor .processLoadFailure (cle .getApplicationContext (), cause );
167
- }
168
- catch (Throwable throwable ) {
169
- if (logger .isDebugEnabled ()) {
170
- logger .debug ("Ignoring exception thrown from ApplicationContextFailureProcessor [%s]: %s"
171
- .formatted (contextFailureProcessor , throwable ));
160
+ catch (Exception ex ) {
161
+ this .contextCache .incrementFailureCount (mergedConfig );
162
+ Throwable cause = ex ;
163
+ if (ex instanceof ContextLoadException cle ) {
164
+ cause = cle .getCause ();
165
+ for (ApplicationContextFailureProcessor contextFailureProcessor : this .contextFailureProcessors ) {
166
+ try {
167
+ contextFailureProcessor .processLoadFailure (cle .getApplicationContext (), cause );
168
+ }
169
+ catch (Throwable throwable ) {
170
+ if (logger .isDebugEnabled ()) {
171
+ logger .debug ("Ignoring exception thrown from ApplicationContextFailureProcessor [%s]: %s"
172
+ .formatted (contextFailureProcessor , throwable ));
173
+ }
172
174
}
173
175
}
174
176
}
177
+ throw new IllegalStateException (
178
+ "Failed to load ApplicationContext for " + mergedConfig , cause );
175
179
}
176
- throw new IllegalStateException (
177
- "Failed to load ApplicationContext for " + mergedConfig , cause );
178
180
}
179
- }
180
- else {
181
- if ( logger .isTraceEnabled ()) {
182
- logger . trace ( "Retrieved ApplicationContext [%s] from cache with key %s" . formatted (
183
- System . identityHashCode ( context ), mergedConfig ));
181
+ else {
182
+ if ( logger . isTraceEnabled ()) {
183
+ logger .trace ( "Retrieved ApplicationContext [%s] from cache with key %s" . formatted (
184
+ System . identityHashCode ( context ), mergedConfig ));
185
+ }
184
186
}
185
187
}
186
-
187
- this .contextCache .logStatistics ();
188
+ finally {
189
+ this .contextCache .logStatistics ();
190
+ }
188
191
189
192
return context ;
190
193
}
0 commit comments