使用Spring自带的定时执行任务工具执行任务

1.首先写一个任务类:

@Component("taskJob")
public class DeleteTableRecord {

    @Scheduled(cron = "0 0 3 * * ?")
    public void job() {
        System.out.println("正在执行任务....");
//要执行的任务
        System.out.println("任务执行完成.");
    }
}

@Scheduled(cron = "0 0 3 * * ?")表示在每天凌晨三点执行任务

@Scheduled(cron = "0 15 3 * * ?")表示每天凌晨三点十五分执行任务


2.在Spring配置文件中加入如下配置:

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-3.0.xsd
      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
      http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">

 


 



...


你可能感兴趣的:(Spring,定时任务,注解,配置)