---
title: "使用spring @Scheduled注解-执行定时任务"
author: "vicky_luo"
date: "2015-05-26T14:36:21Z"
tags: []
categories: ["企业架构"]
slug: "spring-scheduled"
draft: false
lastmod: "2015-10-14T18:19:53Z"
id: 2214448
url: "https://vicky-luo.iteye.com/blog/2214448"
---

01. **第一步：配置spring.xml**
02. &lt;beans
03. xmlns="http://www.springframework.org/schema/beans" 
    
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    
        xmlns:p="http://www.springframework.org/schema/p" 
    
        xmlns:context="http://www.springframework.org/schema/context"
    
        xmlns:mvc="http://www.springframework.org/schema/mvc"
    
        xmlns:task="http://www.springframework.org/schema/task"  
    
        xsi:schemaLocation="http://www.springframework.org/schema/beans
    
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    
        http://www.springframework.org/schema/aop        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    
        http://www.springframework.org/schema/tx  http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    
        http://www.springframework.org/schema/context 
    
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
    
        http://www.springframework.org/schema/mvc 
    
        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    
        http://www.springframework.org/schema/task
    
        http://www.springframework.org/schema/task/spring-task-3.2.xsd"&gt;
04. &lt;context:annotation-config/&gt;
05. &lt;bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /&gt;
06. &lt;task:annotation-driven/&gt;
07. &lt;!--扫描的是com.crm.web这样的包下的内容--&gt;
08. &lt;context:component-scan base-package="com.crm.web" /&gt;
09. **第二步：接口和实现**
10. package com.crm.web;
11. import org.springframework.beans.factory.annotation.Autowired;
12. import org.springframework.scheduling.annotation.Scheduled;
13. import org.springframework.stereotype.Component;
14. **@Component**
15. **public class MyTask {**
16. **@Scheduled(cron="0 0 1 * * ?") //每天1点触发**
17. **//  @Scheduled(cron="0/60 * * * * ? ") //间隔60秒执行**
18. **public void taskCycle(){**
19. **System.out.println("呵呵");**
20. **}**
21. **cron的取值参考：**
22. /\**
23. 秒 0-59 , - * /
24. 分 0-59 , - * /
25. 小时 0-23 , - * /
26. 日期 1-31 , - * ? / L W C
27. 月份 1-12 或者 JAN-DEC , - * /
28. 星期 1-7 或者 SUN-SAT , - * ? / L C #
29. 年（可选）留空, 1970-2099 , - * /
30. 表达式意义
31. "0 0 12 * * ?" 每天中午12点触发
32. "0 15 10 ? * \*" 每天上午10:15触发
33. "0 15 10 * * ?" 每天上午10:15触发
34. "0 15 10 * * ? \*" 每天上午10:15触发
35. "0 15 10 * * ? 2005" 2005年的每天上午10:15触发
36. "0 * 14 * * ?" 在每天下午2点到下午2:59期间的每1分钟触发
37. "0 0/5 14 * * ?" 在每天下午2点到下午2:55期间的每5分钟触发
38. "0 0/5 14,18 * * ?" 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发
39. "0 0-5 14 * * ?" 在每天下午2点到下午2:05期间的每1分钟触发
40. "0 10,44 14 ? 3 WED" 每年三月的星期三的下午2:10和2:44触发
41. "0 15 10 ? * MON-FRI" 周一至周五的上午10:15触发
42. "0 15 10 15 * ?" 每月15日上午10:15触发
43. "0 15 10 L * ?" 每月最后一日的上午10:15触发
44. "0 15 10 ? * 6L" 每月的最后一个星期五上午10:15触发
45. "0 15 10 ? * 6L 2002-2005" 2002年至2005年的每月的最后一个星期五上午10:15触发
46. "0 15 10 ? * 6#3" 每月的第三个星期五上午10:15触发
47. \* \*\*/