spring 配置定时任务Scheduled

一:在spring配置的xml文件添加3条命名空间

xmlns:task="http://www.springframework.org/schema/task" 
 xsi:schemaLocation= "
       http://www.springframework.org/schema/task 
       http://www.springframework.org/schema/task/spring-task-3.0.xsd 
  "

二:在spring配置的xml文件中添加

   <!-- 开启定时任务 -->
    <task:annotation-driven />
    <!-- 开启注解 -->
    <context:annotation-config />

三:在某个定时任务类上添加被扫描的注解@Component,以及在定时执行的方法上添加@Scheduled(cron表达式)

@Component
public class ScheduledTest  {

    @Scheduled(cron="*/3 * * * * ?") //每3秒执行1次
    public void start() {
        System.out.println("每3秒执行一次");
    }

}

四:启动应用,则会在控制台输出

posted on 2018-04-13 15:06  天生一对  阅读(2715)  评论(0编辑  收藏  举报

导航