Consider revisiting the entries above or defining a bean of type 'org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor' in your configuration.
通过@Autowired自动注入ThreadPoolTaskExecutor线程池时报错
研究发现,spring的线程池有两种方式注入
一种是 通过自动注入的方式注入线程池,
@Autowired
private ThreadPoolTaskExecutor applicationTaskExecutor;
这种注入的是默认配置的线程池
另一种是 通过配置类的方式配置线程池,然后@Resource(name)注入
@Configuration
public class ExecturConfig {
@Bean("cacheUpdateTaskExecutor")
public Executor taskExector() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
int i = Runtime.getRuntime().availableProcessors();//获取到服务器的cpu内核
executor.setCorePoolSize(5);//核心池大小
executor.setMaxPoolSize(100);//最大线程数
executor.setQueueCapacity(1000);//队列程度
executor.setKeepAliveSeconds(1000);//线程空闲时间
executor.setThreadNamePrefix("tsak-asyn");//线程前缀名称
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());//配置拒绝策略
return executor;
}
@Resource(name="cacheUpdateTaskExecutor")
private ThreadPoolTaskExecutor applicationTaskExecutor;
上面报错的原因就是我在配置类重写了线程池配置。同时用@Autowired注入时报错,改为@Resource(name="cacheUpdateTaskExecutor")指定名称就好了