(1):修改配置 文件 如下:
<!-- 注解扫描包 -->
<task:annotation-driven/><!-- 用定时器注解 -->
<context:component-scan base-package="com.xx.controller" />
<context:component-scan base-package="com.xx.scheduler" /> <!-- 用定时器 --></span>package com.xx.scheduler;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class Job {
public Job(){
System.out.println("MyJob创建成功");
}
@Scheduled(cron = "0/1 * * * * ? ")//每隔1秒隔行一次
public void run(){
System.out.println("Hello MyJob "+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ").format(new Date()));
}
}
本文介绍如何在Spring框架中配置并使用定时任务。通过修改配置文件启用注解驱动及组件扫描,实现在指定包路径下扫描定时任务注解。此外,还提供了一个具体的定时任务示例,该任务每秒钟执行一次,并打印当前的时间。
367

被折叠的 条评论
为什么被折叠?



