@Configuration @EnableAsync @EnableConfigurationProperties(value=ThreadPoolProperties.class) public class ThreadPoolConfig extends Object implements org.springframework.context.ApplicationListener<org.springframework.boot.web.context.WebServerInitializedEvent>
EnableAsync开启异步支持,
使用注解Async声明异步任务,
异步任务由配置中的线程池调度
二.使用自动注入bean -- @Resource(name = "ThreadPoolTaskExecutor") private ThreadPoolTaskExecutor threadPool; -- @Autowired private ThreadPoolTaskExecutor executorService;
| 构造器和说明 |
|---|
ThreadPoolConfig(ThreadPoolProperties threadPoolProperties) |
| 限定符和类型 | 方法和说明 |
|---|---|
void |
onApplicationEvent(org.springframework.boot.web.context.WebServerInitializedEvent event) |
org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor |
threadPoolTaskExecutor(ThreadPoolProperties poolProperties)
创建自定义线程池
如果 thread-pool.enable 属性不存在,由于 matchIfMissing = true,也视为条件满足,Bean 会被创建。
|
public ThreadPoolConfig(ThreadPoolProperties threadPoolProperties)
@Bean(value="custom-threadpool")
@ConditionalOnProperty(name="thread.pool.enable",
havingValue="true")
public org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor threadPoolTaskExecutor(ThreadPoolProperties poolProperties)
public void onApplicationEvent(org.springframework.boot.web.context.WebServerInitializedEvent event)
onApplicationEvent 在接口中 org.springframework.context.ApplicationListener<org.springframework.boot.web.context.WebServerInitializedEvent>Copyright © 2025. All rights reserved.