第一步:修改applicationContext配置文件java
<!-- 添加一下项 --> <task:annotation-driven/> <context:annotation-config/> <context:component-scan base-package="group.esperanto"/>
第二步:定义执行类spring
package group.esperanto.util; import java.text.SimpleDateFormat; import java.util.Date; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component //必须标记为一个Spring管理的JavaBean类 public class MyTask3 { //不须要继承任何子类 //@Scheduled(fixedRate=2000) //设置为两秒一执行 间隔触发 @Scheduled(cron="* * * * * ?" ) // 一秒一执行 定时触发 public void excJob() { System.out.println("当前日期: " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())); } }