一.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);
    }
}

三.执行结果

SpringTask二:注解配置方式

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-04-06
  • 2022-12-23
  • 2021-11-17
  • 2021-11-23
猜你喜欢
  • 2022-12-23
  • 2021-08-11
  • 2021-12-25
  • 2022-02-17
  • 2022-12-23
  • 2021-07-30
  • 2021-11-18
相关资源
相似解决方案