From 74a014743ad5dccef92eddb5255c75b66c538279 Mon Sep 17 00:00:00 2001 From: Marten Deinum Date: Mon, 10 Aug 2020 12:27:27 +0200 Subject: [PATCH] Call StartupStep.end in finally block Prior to this commit it was possible that a StartupStep was started but never ended. This was the case when an exception occured during bean initializing. To always call the method regardless of the outcome, the call to StartupStep.end has been moved to a finally block. When an exception occurs the StartupStep is also enriched with the exception class and message for diagnostic purposes. See gh-22776 Closes gh-25572 --- .../beans/factory/support/AbstractBeanFactory.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java index 0c0a06247f..841023b55b 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java @@ -300,9 +300,9 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp markBeanAsCreated(beanName); } + StartupStep beanCreation = this.applicationStartup.start("spring.beans.instantiate") + .tag("beanName", name); try { - StartupStep beanCreation = this.applicationStartup.start("spring.beans.instantiate") - .tag("beanName", name); if (requiredType != null) { beanCreation.tag("beanType", requiredType::toString); } @@ -383,12 +383,16 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp throw new ScopeNotActiveException(beanName, scopeName, ex); } } - beanCreation.end(); } catch (BeansException ex) { + beanCreation.tag("exception", ex.getClass().toString()); + beanCreation.tag("message", ex.getMessage()); cleanupAfterBeanCreationFailure(beanName); throw ex; } + finally { + beanCreation.end(); + } } // Check if required type matches the type of the actual bean instance.