work hard work smart

专注于Java后端开发。 不断总结,举一反三。
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Spring Schedule实现定时任务

Posted on 2018-09-09 14:52  work hard work smart  阅读(398)  评论(0编辑  收藏  举报
applicationContext.xml

1. 增加配置

 

 <!-- 开启定时任务 -->
    <task:annotation-driven />
    <!-- 开启注解 -->
    <context:annotation-config />
    <!-- 指定相关的包路径 -->
    <context:component-scan base-package="com.xxx.xxx"/>

  

 

<task:annotation-driven />

 taks来自

http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd



2. 创建定时任务类
@Component
@Slf4j
public class CloseOrderTask {


    @Scheduled(cron="0 */1 * * * ?") //(每个1分钟的整数倍执行一次)
    public  void closeOrderTaskV1(){
        log.info("关闭订单定时任务启动");
        log.info("关闭订单定时任务结束");
    }


}

  

3.在线cron表达式生成器
http://cron.qqe2.com/