我希望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?
答案 0 :(得分:2)
只有在处理该自动配置时,TaskExecutionAutoConfiguration
中的执行者bean(由于@ConditionalOnMissingBean(Executor.class)
)不存在时,才会创建。因此,为了创建我们的执行程序和TaskExecutionAutoConfiguration
中定义的执行程序,我们需要确保在TaskExecutionAutoConfiguration
根据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