zoukankan      html  css  js  c++  java
  • 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);
        }
    }
    

    三.执行结果

  • 相关阅读:
    结构体字节对齐
    ORACLE自增长字段实现
    Oracle 11.2.0.2新特性——用户重命名(Rename User)
    oracle expdp/impdp 用法详解
    sql语句面试题(城市人口统计) .
    ORA30553: 函数不确定!
    SQL Express自动备份 .
    SQL 2005的DES加密算法
    SQL 2005加密数据方法
    CREATE VIEW ORA01031
  • 原文地址:https://www.cnblogs.com/wwjj4811/p/12643252.html
Copyright © 2011-2022 走看看