Merge pull request #14934 from Artsiom Yudovin

* gh-14934:
  Polish "Make auto-configured applicationTaskExecutor bean lazy"
  Make auto-configured applicationTaskExecutor bean lazy
pull/14934/merge
Andy Wilkinson 6 years ago
commit 35bd4cfbed

@ -27,6 +27,7 @@ import org.springframework.boot.task.TaskExecutorBuilder;
import org.springframework.boot.task.TaskExecutorCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.task.TaskDecorator;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
@ -77,6 +78,7 @@ public class TaskExecutionAutoConfiguration {
return builder;
}
@Lazy
@Bean(name = APPLICATION_TASK_EXECUTOR_BEAN_NAME)
@ConditionalOnMissingBean(Executor.class)
public ThreadPoolTaskExecutor applicationTaskExecutor(TaskExecutorBuilder builder) {

@ -20,6 +20,7 @@ import java.util.concurrent.Executor;
import java.util.concurrent.Future;
import java.util.function.Consumer;
import org.junit.Rule;
import org.junit.Test;
import org.springframework.beans.DirectFieldAccessor;
@ -29,6 +30,7 @@ import org.springframework.boot.task.TaskExecutorCustomizer;
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.boot.test.context.runner.ContextConsumer;
import org.springframework.boot.test.rule.OutputCapture;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SyncTaskExecutor;
@ -55,6 +57,9 @@ public class TaskExecutionAutoConfigurationTests {
.withConfiguration(
AutoConfigurations.of(TaskExecutionAutoConfiguration.class));
@Rule
public final OutputCapture output = new OutputCapture();
@Test
public void taskExecutorBuilderShouldApplyCustomSettings() {
this.contextRunner
@ -101,11 +106,15 @@ public class TaskExecutionAutoConfigurationTests {
@Test
public void taskExecutorAutoConfigured() {
this.output.reset();
this.contextRunner.run((context) -> {
assertThat(this.output.toString())
.doesNotContain("Initializing ExecutorService");
assertThat(context).hasSingleBean(Executor.class);
assertThat(context).hasBean("applicationTaskExecutor");
assertThat(context).getBean("applicationTaskExecutor")
.isInstanceOf(ThreadPoolTaskExecutor.class);
assertThat(this.output.toString()).contains("Initializing ExecutorService");
});
}

Loading…
Cancel
Save