一:在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秒執行一次");
}
}
四:啟動應用,則會在控制台輸出