Loading

SpringTask二:注解配置方式

一.applicationContext.xml

pom.xml与第一篇一样

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:task="http://www.springframework.org/schema/task"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/task
        http://www.springframework.org/schema/task/spring-task.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

    <!--开启注解扫描-->
    <context:component-scan base-package="com.wj"/>
    <!--开启对@Scheduled注解的支持-->
    <task:annotation-driven/>

</beans>

二.任务类

@Service
public class TaskService {

    //initialDelay:服务启动后,多少毫秒启动该定时任务
    //fixedDelay:每隔多长时间执行一次定时任务
    //cron:cron表达式,复杂任务
    @Scheduled(initialDelay = 10000,fixedDelay = 1000)
    public void firstTask(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm:ss:ms");
        String format = simpleDateFormat.format(new Date());
        System.out.println("数据库备份时间1:"+format);
    }

    @Scheduled(initialDelay = 20000,fixedDelay = 2000)
    public void secondTask(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm:ss:ms");
        String format = simpleDateFormat.format(new Date());
        System.out.println("数据库备份时间2:"+format);
    }
}

三.执行结果

 

posted @ 2020-04-06 17:56  秋风飒飒吹  阅读(416)  评论(0编辑  收藏  举报