From c2e7f6129aa549088146c7cec2afbda871371f2c Mon Sep 17 00:00:00 2001 From: Johnny Lim Date: Thu, 24 Apr 2025 22:35:53 +0900 Subject: [PATCH] Use ConfigurableApplicationContext.BOOTSTRAP_EXECUTOR_BEAN_NAME See gh-45278 Signed-off-by: Johnny Lim --- .../task/TaskExecutorConfigurations.java | 8 ++--- .../TaskExecutionAutoConfigurationTests.java | 36 ++++++++++++------- 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/task/TaskExecutorConfigurations.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/task/TaskExecutorConfigurations.java index 776f16ccda5..96800cdcb1c 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/task/TaskExecutorConfigurations.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/task/TaskExecutorConfigurations.java @@ -30,6 +30,7 @@ import org.springframework.boot.task.SimpleAsyncTaskExecutorBuilder; import org.springframework.boot.task.SimpleAsyncTaskExecutorCustomizer; import org.springframework.boot.task.ThreadPoolTaskExecutorBuilder; import org.springframework.boot.task.ThreadPoolTaskExecutorCustomizer; +import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Configuration; @@ -167,17 +168,16 @@ class TaskExecutorConfigurations { @Configuration(proxyBeanMethods = false) static class BootstrapExecutorConfiguration { - private static final String BOOTSTRAP_EXECUTOR_NAME = "bootstrapExecutor"; - @Bean static BeanFactoryPostProcessor bootstrapExecutorAliasPostProcessor() { return (beanFactory) -> { - boolean hasBootstrapExecutor = beanFactory.containsBean(BOOTSTRAP_EXECUTOR_NAME); + boolean hasBootstrapExecutor = beanFactory + .containsBean(ConfigurableApplicationContext.BOOTSTRAP_EXECUTOR_BEAN_NAME); boolean hasApplicationTaskExecutor = beanFactory .containsBean(TaskExecutionAutoConfiguration.APPLICATION_TASK_EXECUTOR_BEAN_NAME); if (!hasBootstrapExecutor && hasApplicationTaskExecutor) { beanFactory.registerAlias(TaskExecutionAutoConfiguration.APPLICATION_TASK_EXECUTOR_BEAN_NAME, - BOOTSTRAP_EXECUTOR_NAME); + ConfigurableApplicationContext.BOOTSTRAP_EXECUTOR_BEAN_NAME); } }; } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/task/TaskExecutionAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/task/TaskExecutionAutoConfigurationTests.java index a2c858b9e57..c2242e3a868 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/task/TaskExecutionAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/task/TaskExecutionAutoConfigurationTests.java @@ -39,6 +39,7 @@ import org.springframework.boot.test.context.assertj.AssertableApplicationContex import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.boot.test.context.runner.ContextConsumer; import org.springframework.boot.test.system.OutputCaptureExtension; +import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.task.SimpleAsyncTaskExecutor; @@ -383,21 +384,25 @@ class TaskExecutionAutoConfigurationTests { this.contextRunner.run((context) -> { assertThat(context).hasSingleBean(Executor.class) .hasBean("applicationTaskExecutor") - .hasBean("bootstrapExecutor"); - assertThat(context.getAliases("applicationTaskExecutor")).containsExactly("bootstrapExecutor"); - assertThat(context.getBean("bootstrapExecutor")).isSameAs(context.getBean("applicationTaskExecutor")); + .hasBean(ConfigurableApplicationContext.BOOTSTRAP_EXECUTOR_BEAN_NAME); + assertThat(context.getAliases("applicationTaskExecutor")) + .containsExactly(ConfigurableApplicationContext.BOOTSTRAP_EXECUTOR_BEAN_NAME); + assertThat(context.getBean(ConfigurableApplicationContext.BOOTSTRAP_EXECUTOR_BEAN_NAME)) + .isSameAs(context.getBean("applicationTaskExecutor")); }); } @Test void shouldNotAliasApplicationTaskExecutorWhenBootstrapExecutorIsDefined() { this.contextRunner.withBean("applicationTaskExecutor", Executor.class, () -> createCustomAsyncExecutor("app-")) - .withBean("bootstrapExecutor", Executor.class, () -> createCustomAsyncExecutor("bootstrap-")) + .withBean(ConfigurableApplicationContext.BOOTSTRAP_EXECUTOR_BEAN_NAME, Executor.class, + () -> createCustomAsyncExecutor("bootstrap-")) .run((context) -> { assertThat(context.getBeansOfType(Executor.class)).hasSize(2); - assertThat(context).hasBean("applicationTaskExecutor").hasBean("bootstrapExecutor"); + assertThat(context).hasBean("applicationTaskExecutor") + .hasBean(ConfigurableApplicationContext.BOOTSTRAP_EXECUTOR_BEAN_NAME); assertThat(context.getAliases("applicationTaskExecutor")).isEmpty(); - assertThat(context.getBean("bootstrapExecutor")) + assertThat(context.getBean(ConfigurableApplicationContext.BOOTSTRAP_EXECUTOR_BEAN_NAME)) .isNotSameAs(context.getBean("applicationTaskExecutor")); }); } @@ -408,19 +413,21 @@ class TaskExecutionAutoConfigurationTests { .run((context) -> assertThat(context).hasSingleBean(Executor.class) .hasBean("customExecutor") .doesNotHaveBean("applicationTaskExecutor") - .doesNotHaveBean("bootstrapExecutor")); + .doesNotHaveBean(ConfigurableApplicationContext.BOOTSTRAP_EXECUTOR_BEAN_NAME)); } @Test void shouldNotAliasApplicationTaskExecutorWhenBootstrapExecutorRegisteredAsSingleton() { this.contextRunner.withBean("applicationTaskExecutor", Executor.class, () -> createCustomAsyncExecutor("app-")) .withInitializer((context) -> context.getBeanFactory() - .registerSingleton("bootstrapExecutor", createCustomAsyncExecutor("bootstrap-"))) + .registerSingleton(ConfigurableApplicationContext.BOOTSTRAP_EXECUTOR_BEAN_NAME, + createCustomAsyncExecutor("bootstrap-"))) .run((context) -> { assertThat(context.getBeansOfType(Executor.class)).hasSize(2); - assertThat(context).hasBean("applicationTaskExecutor").hasBean("bootstrapExecutor"); + assertThat(context).hasBean("applicationTaskExecutor") + .hasBean(ConfigurableApplicationContext.BOOTSTRAP_EXECUTOR_BEAN_NAME); assertThat(context.getAliases("applicationTaskExecutor")).isEmpty(); - assertThat(context.getBean("bootstrapExecutor")) + assertThat(context.getBean(ConfigurableApplicationContext.BOOTSTRAP_EXECUTOR_BEAN_NAME)) .isNotSameAs(context.getBean("applicationTaskExecutor")); }); } @@ -430,13 +437,16 @@ class TaskExecutionAutoConfigurationTests { Executor executor = Runnable::run; this.contextRunner.withBean("applicationTaskExecutor", Executor.class, () -> executor) .withBean("customExecutor", Executor.class, () -> createCustomAsyncExecutor("custom")) - .withInitializer((context) -> context.getBeanFactory().registerAlias("customExecutor", "bootstrapExecutor")) + .withInitializer((context) -> context.getBeanFactory() + .registerAlias("customExecutor", ConfigurableApplicationContext.BOOTSTRAP_EXECUTOR_BEAN_NAME)) .run((context) -> { assertThat(context.getBeansOfType(Executor.class)).hasSize(2); assertThat(context).hasBean("applicationTaskExecutor").hasBean("customExecutor"); assertThat(context.getAliases("applicationTaskExecutor")).isEmpty(); - assertThat(context.getAliases("customExecutor")).contains("bootstrapExecutor"); - assertThat(context.getBean("bootstrapExecutor")).isNotSameAs(context.getBean("applicationTaskExecutor")) + assertThat(context.getAliases("customExecutor")) + .contains(ConfigurableApplicationContext.BOOTSTRAP_EXECUTOR_BEAN_NAME); + assertThat(context.getBean(ConfigurableApplicationContext.BOOTSTRAP_EXECUTOR_BEAN_NAME)) + .isNotSameAs(context.getBean("applicationTaskExecutor")) .isSameAs(context.getBean("customExecutor")); }); }