参考地址: https://blog.csdn.net/u011734144/article/details/52849868
代码:
@Component
public class QuartzService implements InitializingBean {
@Scheduled(cron = "0 10 21 * * ?")
public void timerToNow(){
System.out.println("now time:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
}
@Override
public void afterPropertiesSet() throws Exception {
this.timerToNow();
}
}
解决:类上添加注解:@EnableScheduling即可解决
PS:实现 InitializingBean是为了在系统启动的时候调用定时方法,启动过程中执行一次