Skip to content

Commit ab1b310

Browse files
committed
Polish "Allow configuring a TaskExecutor even if an Executor is present"
See gh-44659
1 parent c5eb394 commit ab1b310

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

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

+1-3
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
import org.springframework.core.task.SimpleAsyncTaskExecutor;
3838
import org.springframework.core.task.TaskDecorator;
3939
import org.springframework.core.task.TaskExecutor;
40-
import org.springframework.scheduling.annotation.AsyncAnnotationBeanPostProcessor;
4140
import org.springframework.scheduling.annotation.AsyncConfigurer;
4241
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
4342

@@ -146,8 +145,7 @@ private SimpleAsyncTaskExecutorBuilder builder() {
146145
}
147146

148147
@Configuration(proxyBeanMethods = false)
149-
@ConditionalOnMissingBean(name = AsyncAnnotationBeanPostProcessor.DEFAULT_TASK_EXECUTOR_BEAN_NAME,
150-
value = AsyncConfigurer.class)
148+
@ConditionalOnMissingBean(AsyncConfigurer.class)
151149
static class AsyncConfigurerConfiguration {
152150

153151
@Bean

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

+19-3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.junit.jupiter.api.extension.ExtendWith;
3131

3232
import org.springframework.beans.factory.config.BeanDefinition;
33+
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
3334
import org.springframework.beans.factory.support.BeanDefinitionOverrideException;
3435
import org.springframework.boot.autoconfigure.AutoConfigurations;
3536
import org.springframework.boot.task.SimpleAsyncTaskExecutorBuilder;
@@ -239,6 +240,21 @@ void taskExecutorWhenModeIsForceAndHasCustomTaskExecutorWithReservedNameShouldTh
239240
.isInstanceOf(BeanDefinitionOverrideException.class));
240241
}
241242

243+
@Test
244+
void taskExecutorWhenModeIsForceAndHasCustomBFPPCanRestoreTaskExecutorAlias() {
245+
this.contextRunner.withBean("customTaskExecutor", Executor.class, SyncTaskExecutor::new)
246+
.withPropertyValues("spring.task.execution.mode=force")
247+
.withBean(BeanFactoryPostProcessor.class,
248+
() -> (beanFactory) -> beanFactory.registerAlias("applicationTaskExecutor", "taskExecutor"))
249+
.run((context) -> {
250+
assertThat(context.getBeansOfType(Executor.class)).hasSize(2)
251+
.containsKeys("customTaskExecutor", "applicationTaskExecutor");
252+
assertThat(context).hasBean("taskExecutor");
253+
assertThat(context.getBean("taskExecutor")).isSameAs(context.getBean("applicationTaskExecutor"));
254+
255+
});
256+
}
257+
242258
@Test
243259
@EnabledForJreRange(min = JRE.JAVA_21)
244260
void whenVirtualThreadsAreEnabledAndCustomTaskExecutorIsDefinedThenSimpleAsyncTaskExecutorThatUsesVirtualThreadsBacksOff() {
@@ -294,18 +310,18 @@ void enableAsyncUsesAutoConfiguredExecutorWhenModeIsForceAndHasCustomTaskExecuto
294310
}
295311

296312
@Test
297-
void enableAsyncUsesCustomExecutorWhenModeIsForceAndHasCustomTaskExecutorWithReservedName() {
313+
void enableAsyncUsesAutoConfiguredExecutorWhenModeIsForceAndHasCustomTaskExecutorWithReservedName() {
298314
this.contextRunner
299315
.withPropertyValues("spring.task.execution.thread-name-prefix=auto-task-",
300316
"spring.task.execution.mode=force")
301317
.withBean("taskExecutor", Executor.class, () -> createCustomAsyncExecutor("custom-task-"))
302318
.withUserConfiguration(AsyncConfiguration.class, TestBean.class)
303319
.run((context) -> {
304-
assertThat(context).doesNotHaveBean(AsyncConfigurer.class);
320+
assertThat(context).hasSingleBean(AsyncConfigurer.class);
305321
assertThat(context.getBeansOfType(Executor.class)).hasSize(2);
306322
TestBean bean = context.getBean(TestBean.class);
307323
String text = bean.echo("something").get();
308-
assertThat(text).contains("custom-task-").contains("something");
324+
assertThat(text).contains("auto-task-").contains("something");
309325
});
310326
}
311327

0 commit comments

Comments
 (0)