是否可以注入TaskScheduler
Spring创建的实例?
我想以编程方式安排任务,为此,我想我需要访问TaskScheduler
但由于某种原因,Spring没有找到自动装配.
@Configuration @EnableScheduling public class MySpringConfig { } @Component public class MyClass implements InitializingBean { @Autowired private TaskScheduler taskScheduler; @Override public void afterPropertiesSet() throws Exception { ... } }
任何的想法?
谢谢!
@Configuration @EnableScheduling public class MySpringConfig { @Bean public TaskScheduler taskScheduler() { //org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler return new ThreadPoolTaskScheduler(); } }
您可以选择您喜欢的实施方式.ThreadPoolTaskScheduler是这个链接中提到的更简单的一个.
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/scheduling.html#scheduling-task-scheduler-implementations