【JavaWeb】SpringMVC定时任务

1.配置文件添加

xmlns:task="http://www.springframework.org/schema/task" 
http://www.springframework.org/schema/task  
http://www.springframework.org/schema/task/spring-task-3.2.xsd

2.配置任务扫描

<task:annotation-driven />

3.配置扫描任务位置

    <context:component-scan base-package="com.vrveis.roundTrip.task" />

4.任务实现类

@Component
public class FlightTrainTask {
    @Scheduled(cron = "0 0 */2 * * ? ") // 间隔2小时执行
    public void taskCycle() {
        System.out.println("定时任务");
    }
}
  • 必须声明@Component
  • 0 0 */2 * * ? 为秒、分、时….

详细参照:SpringMVC定时任务

原文地址:https://www.cnblogs.com/cnsec/p/13286768.html