不想写繁琐的xml文件,所以试着用Spring的注解来配置定时任务。按网上好多写的那样,使用@Scheduled注解:
@Scheduled(fixedRate=5000)
public void doSomething() {
// something that should execute periodically
}
但是怎么也无法让定时任务跑起来。最后无奈,只有去官网看英语了。
原来,还需要有一个允许定时任务的注解,放在一个有@configuration注解类中。
@Configuration
@EnableAsync
@EnableScheduling
public class AppConfig {
}
其中,@EnableAsysnc是允许异步任务。至于@Configuration的说明,见Spring的最新文档。