如何在TaskExecutionAutoConfiguration旁边创建其他TaskExecutor?

时间:2019-01-08 14:20:24

标签: java spring spring-boot

我希望spring从 var fileGroup = (from file in Directory.EnumerateFiles(myPath, searchPattern: "*.xlsx", searchOption: SearchOption.AllDirectories) let fileName = Path.GetFileName(file) select fileName) .GroupBy(f => f.Substring(f.LastIndexOf("_") + 1)) .Select(x => x.OrderBy(y => y)); 加载默认的ThreadPoolTaskExecutor。尽管我想为某些明确的辅助任务提供额外的执行程序:

TaskExecutionAutoConfiguration

问题:在上面添加Bean,将不再执行@Bean public ThreadPoolExecutor myRequestPool() { return (ThreadPoolExecutor) Executors.newFixedThreadPool(10); } ,并且由于TaskExecutionAutoConfiguration不再匹配而不会初始化spring-default执行程序:

@ConditionalOnMissingBean(Executor.class)

问题:我还如何让spring来创建两个bean?

1 个答案:

答案 0 :(得分:2)

只有在处理该自动配置时,TaskExecutionAutoConfiguration中的执行者bean(由于@ConditionalOnMissingBean(Executor.class))不存在时,才会创建。因此,为了创建我们的执行程序和TaskExecutionAutoConfiguration中定义的执行程序,我们需要确保在TaskExecutionAutoConfiguration

之后处理我们的bean。

根据docs,如果我们使bean成为自动配置候选对象(需要在@Configuration中添加META-INF/spring.factories类),则可以使用{{1} },将其配置为在@AutoConfigureAfter之后进行处理:

TaskExecutionAutoConfiguration

然后创建包含以下内容的package foo.bar.baz.qux; @Configuration @AutoConfigureAfter(TaskExecutionAutoConfiguration.class) public class Config { @Bean public ThreadPoolExecutor myRequestPool() { return (ThreadPoolExecutor) Executors.newFixedThreadPool(10); } }

META-INF/spring.factories