关于Spring的定时任务

来源:互联网 发布:react.js中文 编辑:程序博客网 时间:2024/05/19 18:00

首先,创建一个String-task.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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"
default-lazy-init="false"
>
 
<context:annotation-config />  
   <!--  <!—spring扫描注解的配置   -->  
  <context:component-scan base-package="com.mabang.zstask" />    这个为包的路径
      
  <!-- 开启这个配置,spring才能识别@Scheduled注解    -->
    <task:annotation-driven scheduler="qbScheduler" mode="proxy"/>  
    <task:scheduler id="qbScheduler" pool-size="10"/>  
    
</beans>




----------------------------------------------------------------

我是直接从项目里复制过来的,不用的可以删掉


package com.mabang.zstask;


import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;









@Component("taskJob")  
public class TaskJob {  
@Autowired
private PurchaseService purchaseService;
@Autowired
private ProductService productService;


    @Scheduled(cron = "0 0/5 * * * ?")   每5分钟执行一次
    public void job1() {     
    System.out.println("同步采购人员"+new Date());
   
   
    }  
    
    @Scheduled(cron = "0 10 7 * * ?")  每天早上7点10执行一次
    public void product(){
   

   
    }
    
    @Scheduled(cron = "0 05 13 * * ?")   每天下午1点05执行一次
    public void product3(){
    purchaseService.deleteTemManu();

   
    }
  //@Scheduled(cron = "0/10 * * * * ?")   每10秒执行一次
    public void product1(){
  System.out.println("呵呵");
 
  
   
    }
}  


--------------------------------------------------------------------------


好了,这些是直接拿去就能用的,不明白的地方可以留言问我,这些感觉都是比较常用到的,很方便



原创粉丝点击