From 1e97ff834e2cf303c152ca91be33b0cc067b0729 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Fri, 19 Jun 2020 08:19:57 +0100 Subject: [PATCH] Only consider current context when finding lifecycle processor Previously, LifecycleAutoConfiguration would check the current context and all of its ancestors for a lifecycle processor bean, only configuring a custom processor if one was not found. Every context has a lifecycle processor so this check meant that lifecycle processing timeout could not be customized in any context with a parent. This commit updates the auto-configuration to only check the current context. Closes gh-22014 --- .../context/LifecycleAutoConfiguration.java | 4 +++- .../context/LifecycleAutoConfigurationTests.java | 12 ++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/context/LifecycleAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/context/LifecycleAutoConfiguration.java index a3ceb5a91f..70ade77b05 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/context/LifecycleAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/context/LifecycleAutoConfiguration.java @@ -18,6 +18,7 @@ package org.springframework.boot.autoconfigure.context; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.SearchStrategy; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -36,7 +37,8 @@ import org.springframework.context.support.DefaultLifecycleProcessor; public class LifecycleAutoConfiguration { @Bean(name = AbstractApplicationContext.LIFECYCLE_PROCESSOR_BEAN_NAME) - @ConditionalOnMissingBean(name = AbstractApplicationContext.LIFECYCLE_PROCESSOR_BEAN_NAME) + @ConditionalOnMissingBean(name = AbstractApplicationContext.LIFECYCLE_PROCESSOR_BEAN_NAME, + search = SearchStrategy.CURRENT) public DefaultLifecycleProcessor defaultLifecycleProcessor(LifecycleProperties properties) { DefaultLifecycleProcessor lifecycleProcessor = new DefaultLifecycleProcessor(); lifecycleProcessor.setTimeoutPerShutdownPhase(properties.getTimeoutPerShutdownPhase().toMillis()); diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/context/LifecycleAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/context/LifecycleAutoConfigurationTests.java index 8b80f5ed4c..f34f7c3999 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/context/LifecycleAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/context/LifecycleAutoConfigurationTests.java @@ -55,6 +55,18 @@ public class LifecycleAutoConfigurationTests { }); } + @Test + void lifecycleProcessorIsConfiguredWithCustomDefaultTimeoutInAChildContext() { + new ApplicationContextRunner().run((parent) -> { + this.contextRunner.withParent(parent).withPropertyValues("spring.lifecycle.timeout-per-shutdown-phase=15s") + .run((child) -> { + assertThat(child).hasBean(AbstractApplicationContext.LIFECYCLE_PROCESSOR_BEAN_NAME); + Object processor = child.getBean(AbstractApplicationContext.LIFECYCLE_PROCESSOR_BEAN_NAME); + assertThat(processor).extracting("timeoutPerShutdownPhase").isEqualTo(15000L); + }); + }); + } + @Test void whenUserDefinesALifecycleProcessorBeanThenTheAutoConfigurationBacksOff() { this.contextRunner.withUserConfiguration(LifecycleProcessorConfiguration.class).run((context) -> {