Skip to content

Commit cabc261

Browse files
committed
Merge pull request #45278 from izeye
* gh-45278: Use ConfigurableApplicationContext.BOOTSTRAP_EXECUTOR_BEAN_NAME Closes gh-45278
2 parents 306524a + c2e7f61 commit cabc261

File tree

2 files changed

+27
-17
lines changed

2 files changed

+27
-17
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/task/TaskExecutorConfigurations.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.springframework.boot.task.SimpleAsyncTaskExecutorCustomizer;
3131
import org.springframework.boot.task.ThreadPoolTaskExecutorBuilder;
3232
import org.springframework.boot.task.ThreadPoolTaskExecutorCustomizer;
33+
import org.springframework.context.ConfigurableApplicationContext;
3334
import org.springframework.context.annotation.Bean;
3435
import org.springframework.context.annotation.Conditional;
3536
import org.springframework.context.annotation.Configuration;
@@ -167,17 +168,16 @@ public Executor getAsyncExecutor() {
167168
@Configuration(proxyBeanMethods = false)
168169
static class BootstrapExecutorConfiguration {
169170

170-
private static final String BOOTSTRAP_EXECUTOR_NAME = "bootstrapExecutor";
171-
172171
@Bean
173172
static BeanFactoryPostProcessor bootstrapExecutorAliasPostProcessor() {
174173
return (beanFactory) -> {
175-
boolean hasBootstrapExecutor = beanFactory.containsBean(BOOTSTRAP_EXECUTOR_NAME);
174+
boolean hasBootstrapExecutor = beanFactory
175+
.containsBean(ConfigurableApplicationContext.BOOTSTRAP_EXECUTOR_BEAN_NAME);
176176
boolean hasApplicationTaskExecutor = beanFactory
177177
.containsBean(TaskExecutionAutoConfiguration.APPLICATION_TASK_EXECUTOR_BEAN_NAME);
178178
if (!hasBootstrapExecutor && hasApplicationTaskExecutor) {
179179
beanFactory.registerAlias(TaskExecutionAutoConfiguration.APPLICATION_TASK_EXECUTOR_BEAN_NAME,
180-
BOOTSTRAP_EXECUTOR_NAME);
180+
ConfigurableApplicationContext.BOOTSTRAP_EXECUTOR_BEAN_NAME);
181181
}
182182
};
183183
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/task/TaskExecutionAutoConfigurationTests.java

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
4040
import org.springframework.boot.test.context.runner.ContextConsumer;
4141
import org.springframework.boot.test.system.OutputCaptureExtension;
42+
import org.springframework.context.ConfigurableApplicationContext;
4243
import org.springframework.context.annotation.Bean;
4344
import org.springframework.context.annotation.Configuration;
4445
import org.springframework.core.task.SimpleAsyncTaskExecutor;
@@ -383,21 +384,25 @@ void shouldAliasApplicationTaskExecutorToBootstrapExecutor() {
383384
this.contextRunner.run((context) -> {
384385
assertThat(context).hasSingleBean(Executor.class)
385386
.hasBean("applicationTaskExecutor")
386-
.hasBean("bootstrapExecutor");
387-
assertThat(context.getAliases("applicationTaskExecutor")).containsExactly("bootstrapExecutor");
388-
assertThat(context.getBean("bootstrapExecutor")).isSameAs(context.getBean("applicationTaskExecutor"));
387+
.hasBean(ConfigurableApplicationContext.BOOTSTRAP_EXECUTOR_BEAN_NAME);
388+
assertThat(context.getAliases("applicationTaskExecutor"))
389+
.containsExactly(ConfigurableApplicationContext.BOOTSTRAP_EXECUTOR_BEAN_NAME);
390+
assertThat(context.getBean(ConfigurableApplicationContext.BOOTSTRAP_EXECUTOR_BEAN_NAME))
391+
.isSameAs(context.getBean("applicationTaskExecutor"));
389392
});
390393
}
391394

392395
@Test
393396
void shouldNotAliasApplicationTaskExecutorWhenBootstrapExecutorIsDefined() {
394397
this.contextRunner.withBean("applicationTaskExecutor", Executor.class, () -> createCustomAsyncExecutor("app-"))
395-
.withBean("bootstrapExecutor", Executor.class, () -> createCustomAsyncExecutor("bootstrap-"))
398+
.withBean(ConfigurableApplicationContext.BOOTSTRAP_EXECUTOR_BEAN_NAME, Executor.class,
399+
() -> createCustomAsyncExecutor("bootstrap-"))
396400
.run((context) -> {
397401
assertThat(context.getBeansOfType(Executor.class)).hasSize(2);
398-
assertThat(context).hasBean("applicationTaskExecutor").hasBean("bootstrapExecutor");
402+
assertThat(context).hasBean("applicationTaskExecutor")
403+
.hasBean(ConfigurableApplicationContext.BOOTSTRAP_EXECUTOR_BEAN_NAME);
399404
assertThat(context.getAliases("applicationTaskExecutor")).isEmpty();
400-
assertThat(context.getBean("bootstrapExecutor"))
405+
assertThat(context.getBean(ConfigurableApplicationContext.BOOTSTRAP_EXECUTOR_BEAN_NAME))
401406
.isNotSameAs(context.getBean("applicationTaskExecutor"));
402407
});
403408
}
@@ -408,19 +413,21 @@ void shouldNotAliasApplicationTaskExecutorWhenApplicationTaskExecutorIsMissing()
408413
.run((context) -> assertThat(context).hasSingleBean(Executor.class)
409414
.hasBean("customExecutor")
410415
.doesNotHaveBean("applicationTaskExecutor")
411-
.doesNotHaveBean("bootstrapExecutor"));
416+
.doesNotHaveBean(ConfigurableApplicationContext.BOOTSTRAP_EXECUTOR_BEAN_NAME));
412417
}
413418

414419
@Test
415420
void shouldNotAliasApplicationTaskExecutorWhenBootstrapExecutorRegisteredAsSingleton() {
416421
this.contextRunner.withBean("applicationTaskExecutor", Executor.class, () -> createCustomAsyncExecutor("app-"))
417422
.withInitializer((context) -> context.getBeanFactory()
418-
.registerSingleton("bootstrapExecutor", createCustomAsyncExecutor("bootstrap-")))
423+
.registerSingleton(ConfigurableApplicationContext.BOOTSTRAP_EXECUTOR_BEAN_NAME,
424+
createCustomAsyncExecutor("bootstrap-")))
419425
.run((context) -> {
420426
assertThat(context.getBeansOfType(Executor.class)).hasSize(2);
421-
assertThat(context).hasBean("applicationTaskExecutor").hasBean("bootstrapExecutor");
427+
assertThat(context).hasBean("applicationTaskExecutor")
428+
.hasBean(ConfigurableApplicationContext.BOOTSTRAP_EXECUTOR_BEAN_NAME);
422429
assertThat(context.getAliases("applicationTaskExecutor")).isEmpty();
423-
assertThat(context.getBean("bootstrapExecutor"))
430+
assertThat(context.getBean(ConfigurableApplicationContext.BOOTSTRAP_EXECUTOR_BEAN_NAME))
424431
.isNotSameAs(context.getBean("applicationTaskExecutor"));
425432
});
426433
}
@@ -430,13 +437,16 @@ void shouldNotAliasApplicationTaskExecutorWhenBootstrapExecutorAliasIsDefined()
430437
Executor executor = Runnable::run;
431438
this.contextRunner.withBean("applicationTaskExecutor", Executor.class, () -> executor)
432439
.withBean("customExecutor", Executor.class, () -> createCustomAsyncExecutor("custom"))
433-
.withInitializer((context) -> context.getBeanFactory().registerAlias("customExecutor", "bootstrapExecutor"))
440+
.withInitializer((context) -> context.getBeanFactory()
441+
.registerAlias("customExecutor", ConfigurableApplicationContext.BOOTSTRAP_EXECUTOR_BEAN_NAME))
434442
.run((context) -> {
435443
assertThat(context.getBeansOfType(Executor.class)).hasSize(2);
436444
assertThat(context).hasBean("applicationTaskExecutor").hasBean("customExecutor");
437445
assertThat(context.getAliases("applicationTaskExecutor")).isEmpty();
438-
assertThat(context.getAliases("customExecutor")).contains("bootstrapExecutor");
439-
assertThat(context.getBean("bootstrapExecutor")).isNotSameAs(context.getBean("applicationTaskExecutor"))
446+
assertThat(context.getAliases("customExecutor"))
447+
.contains(ConfigurableApplicationContext.BOOTSTRAP_EXECUTOR_BEAN_NAME);
448+
assertThat(context.getBean(ConfigurableApplicationContext.BOOTSTRAP_EXECUTOR_BEAN_NAME))
449+
.isNotSameAs(context.getBean("applicationTaskExecutor"))
440450
.isSameAs(context.getBean("customExecutor"));
441451
});
442452
}

0 commit comments

Comments
 (0)