Clean resources in case of unexpected exception
This commit updates AbstractApplicationContext#refresh to handle any exceptions, not only BeansExceptions. Closes gh-28878
This commit is contained in:
@@ -619,12 +619,11 @@ public abstract class AbstractApplicationContext extends DefaultResourceLoader
|
||||
finishRefresh();
|
||||
}
|
||||
|
||||
catch (BeansException ex) {
|
||||
catch (RuntimeException | Error ex ) {
|
||||
if (logger.isWarnEnabled()) {
|
||||
logger.warn("Exception encountered during context initialization - " +
|
||||
"cancelling refresh attempt: " + ex);
|
||||
}
|
||||
|
||||
// Destroy already created singletons to avoid dangling resources.
|
||||
destroyBeans();
|
||||
|
||||
@@ -974,7 +973,7 @@ public abstract class AbstractApplicationContext extends DefaultResourceLoader
|
||||
* after an exception got thrown.
|
||||
* @param ex the exception that led to the cancellation
|
||||
*/
|
||||
protected void cancelRefresh(BeansException ex) {
|
||||
protected void cancelRefresh(Throwable ex) {
|
||||
this.active.set(false);
|
||||
|
||||
// Reset common introspection caches in Spring's core infrastructure.
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2020 the original author or authors.
|
||||
* Copyright 2002-2023 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -136,7 +136,7 @@ public abstract class AbstractRefreshableApplicationContext extends AbstractAppl
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void cancelRefresh(BeansException ex) {
|
||||
protected void cancelRefresh(Throwable ex) {
|
||||
DefaultListableBeanFactory beanFactory = this.beanFactory;
|
||||
if (beanFactory != null) {
|
||||
beanFactory.setSerializationId(null);
|
||||
|
||||
@@ -297,7 +297,7 @@ public class GenericApplicationContext extends AbstractApplicationContext implem
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void cancelRefresh(BeansException ex) {
|
||||
protected void cancelRefresh(Throwable ex) {
|
||||
this.beanFactory.setSerializationId(null);
|
||||
super.cancelRefresh(ex);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user