Is it possible to inject the TaskScheduler
instance created by Spring?
I would like to schedule tasks programatically and for that, I guess I need to access the TaskScheduler
but for some reason, it's not found by Spring for autowiring.
@Configuration
@EnableScheduling
public class MySpringConfig {
}
@Component
public class MyClass implements InitializingBean {
@Autowired
private TaskScheduler taskScheduler;
@Override
public void afterPropertiesSet() throws Exception {
...
}
}
Any idea?
Thanks!
@Configuration
@EnableScheduling
public class MySpringConfig {
@Bean
public TaskScheduler taskScheduler() {
//org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler
return new ThreadPoolTaskScheduler();
}
}
You can choose which ever implementation you like. ThreadPoolTaskScheduler is the simpler one as mentioned in this link.
http://docs.spring.io/spring/docs/current/spring-framework-reference/html/scheduling.html#scheduling-task-scheduler-implementations
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With