@Component publicclassSpringTaskDemo{ @Scheduled(cron = "0/2 * * * * ?") publicvoidjob1(){ System.out.println("Thread Name : " + Thread.currentThread().getName() + " i am a job1(注解配置方式) : date -> " + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); } }
测试定时任务
1 2 3 4 5
publicstaticvoidmain(String[] args){ System.out.println("定义任务测试..."); // 获取Spring上下文环境 ApplicationContext ac = new ClassPathXmlApplicationContext("spring/spring-task.xml"); }
2、通过XML配置方式实现定时任务
定时任务方法
1 2 3 4 5 6
publicvoidjob2(){ System.out.println("Thread Name : " + Thread.currentThread().getName() + " i am a job2(XML配置方式) : date -> " + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); }