首先在xml配置文件头中添加以下几行:
 
 
在xml中添加配置:<task:annotation-driven />
 
配置自动扫描:
<context:component-scan base-package="com.practice.*" />
 
在要进行定时任务的类前加上 @Component
在定时任务的方法上加上 @Scheduled(corn = "")
corn的内容下面介绍
完成后启动项目,springMVC会自动扫描并完成定时任务。我的例子如下:
 
 package com.practice.prac.service.Impl;

 import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date; import javax.annotation.Resource; import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service; import com.practice.prac.dao.CreateTableByDayMapper;
import com.practice.prac.service.CreateTableByDayService; @Service("CreateTableByDayService")
@Component
public class CreateTableByDayImpl implements CreateTableByDayService { @Resource
CreateTableByDayMapper createTableByDayMapper; @Override
@Scheduled(cron = "*/5 * * * * ?")
public void createTable() { DateFormat fmt = new SimpleDateFormat("yyyyMMdd");
String time = fmt.format(new Date());
String sql = "create table if not exists table_name_" + time
+ " (name varchar(50),value varchar(50))";
createTableByDayMapper.createTable(sql);
} }

CreateTableByDyImpl.java

corn的配置情况如下:
CRON表达式  含义 
"0 0 12 * * ?"   每天中午十二点触发 
"0 15 10 ? * *"   每天早上10:15触发 
"0 15 10 * * ?"   每天早上10:15触发 
"0 15 10 * * ? *"  每天早上10:15触发 
"0 15 10 * * ? 2005"  2005年的每天早上10:15触发 
"0 * 14 * * ?"   每天从下午2点开始到2点59分每分钟一次触发 
"0 0/5 14 * * ?"  每天从下午2点开始到2:55分结束每5分钟一次触发 
"0 0/5 14,18 * * ?"  每天的下午2点至2:55和6点至6点55分两个时间段内每5分钟一次触发 
"0 0-5 14 * * ?"   每天14:00至14:05每分钟一次触发 
"0 10,44 14 ? 3 WED" 三月的每周三的14:10和14:44触发 
"0 15 10 ? * MON-FRI" 每个周一、周二、周三、周四、周五的10:15触发
 
具体参见这边博客:http://biaoming.iteye.com/blog/39532

使用SpringMVC自带的@Scheduled完成定时任务的更多相关文章

  1. spring的Scheduled(定时任务)和多线程

    一.前言 在我们日常的开发中,经常用到数据同步的更新,这时我们采用的是spring的定时任务和java的多线程进行数据的更新,进行时实的服务调用. 二.实现思路            1.创建线程类 ...

  2. spring注解scheduled实现定时任务

    只想说,spring注解scheduled实现定时任务使用真的非常简单. 一.配置spring.xml文件 1.在beans加入xmlns:task="http://www.springfr ...

  3. 定时任务-----Springboot中使用Scheduled做定时任务----http://www.cnblogs.com/lirenqing/p/6596557.html

    Springboot中使用Scheduled做定时任务---http://www.cnblogs.com/lirenqing/p/6596557.html 已经验证的方案: pom文件加入依赖 < ...

  4. SpringBoot中使用@Scheduled创建定时任务

    SpringBoot中使用@Scheduled创建定时任务 定时任务一般会在很多项目中都会用到,我们往往会间隔性的的去完成某些特定任务来减少服务器和数据库的压力.比较常见的就是金融服务系统推送回调,一 ...

  5. SpringBoot使用@Scheduled创建定时任务

    定时任务一般会存在中大型企业级项目中,为了减少服务器.数据库的压力往往会采用时间段性的去完成某些业务逻辑.比较常见的就是金融服务系统推送回调,一般支付系统订单在没有收到成功的回调返回内容时会持续性的回 ...

  6. 使用Spring的@Scheduled实现定时任务

    Spring配置文件xmlns加入xmlns:task="http://www.springframework.org/schema/task"xsi:schemaLocation ...

  7. Springboot中使用Scheduled做定时任务

    在开发中,定时任务是常见的功能,在spring boot 下开发定时任务其实很简单,具体代码如下: 1.配置依赖包pom.xml 由于默认的maven仓库经常访问不了,这里采用了阿里云的maven仓库 ...

  8. 使用Spring的@Scheduled实现定时任务参数详解

    Spring配置文件xmlns加入 xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocati ...

  9. SpringBoot之使用Scheduled做定时任务

    定时任务有好多开源框架比如Quartz,@Scheduled是Spring的一个定时任务注解,通过注解配置就能够轻量级的定时任务,简单方便. 一.@Scheduled注解介绍 这里先贴上@Schedu ...

随机推荐

  1. OneThink生成分类树方法(list_to_tree)使用!

    具体方法: Application / Common / Common / function.php 下的 224行: function list_to_tree($list, $pk='id', $ ...

  2. 次小生成树(poj1679)

    The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20737   Accepted: 7281 D ...

  3. (贪心)kruskal思想

    hdu4313 Matrix Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  4. Web安全开发建议

    版权声明:原创作品,如需转载,请与作者联系.否则将追究法律责任. Web安全问题,很多时候会被人所忽略,安全漏洞造成了很多不必要的维护和开发任务,产生的问题有时候更是致命的. 实际上,只要我们养成一些 ...

  5. mongdb ---shard

    http://blog.fens.me/mongodb-shard/ https://segmentfault.com/a/1190000004263332 1. 和用户管理相关的操作基本都要在adm ...

  6. 使用 Nginx 提升网站访问速度

    使用 Nginx 提升网站访问速度 http://www.ibm.com/developerworks/cn/web/wa-lo-nginx/ Nginx 简介 Nginx ("engine ...

  7. 用nginx的反向代理机制解决前端跨域问题在nginx上部署web静态页面

    用nginx的反向代理机制解决前端跨域问题在nginx上部署web静态页面 1.什么是跨域以及产生原因 跨域是指a页面想获取b页面资源,如果a.b页面的协议.域名.端口.子域名不同,或是a页面为ip地 ...

  8. [C#]嵌入互操作类型

    嵌入互操作类型(Embed Interop Types) 运用office编程调用Excel 的PIA时Microsoft.Office.Interop.Excel.dll时会产生如下问题: 1.提示 ...

  9. github使用方法

    1:fork别人的项目后,更新别人最新的提交 https://blog.csdn.net/qq1332479771/article/details/56087333 2:在idea中push工程    ...

  10. fold change的意义[转载]

    转自:https://zhidao.baidu.com/question/2052933434631672387.html 1.解释 解释:表达值倍数变化 ,分析,消除可能的混杂因素,必要时可以用读段 ...