一、首先配置applicationContext-task.xml

  (1)添加 xmlns:task="http://www.springframework.org/schema/task"

  (2)添加 xsi:schemaLocation="http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd"

  ------- applicationContext-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:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd">
<description>spring task定时任务</description>
<!-- 定时任务配置 scheduler 方式 注解 -->
<context:component-scan base-package="com.lwj.task" />
<task:executor id="executor" pool-size="5" />
<task:scheduler id="scheduler" pool-size="10" />
<task:annotation-driven executor="executor"
scheduler="scheduler" />
</beans>

二、配置applicationContext.xml文件

  ------- applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<!-- 开启注解扫描 -->
<context:annotation-config /> <!--引入配置属性文件 -->
<context:property-placeholder location="classpath:config.properties" /> <!--自动扫描含有@Service将其注入为bean 和@Repository注入 -->
<context:component-scan base-package="com.lwj.service,com.lwj.dao" />
<!-- 定时任务 -->
<import resource="applicationContext-task.xml" />
</beans>

三、编写TimeTask.java

  ------- TimeTask.java文件 --------

package com.lwj.task;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component; /**
*
* @Description : 定时任务
* @author : lwj
* @version : 1.0
* @Date : 2016年4月7日 上午8:56:00
*/
@Component
public class TimeTask {
@Scheduled(cron="0/10 * * * * *")
public void Test(){
System.out.println("每10秒执行一次任务!");
}
}

效果图:

最新文章

  1. AEAI CRM_v1.5.2升级说明,开源客户关系管理系统
  2. log4php的配置
  3. AC日记——鬼谷子的钱袋 codevs 2998
  4. ubuntu 14.04 对exfat的支持
  5. HUDSON(Java开发的一种持续集成工具)
  6. 20145208 《Java程序设计》第6周学习总结
  7. 理解和熟练运用js中的call及apply
  8. AQL Subset Compiler:手把手教你如何写一个完整的编译器
  9. freemarker定义自己的标签错误(八)
  10. localStorage与sessionStorage区别
  11. Python菜鸟快乐游戏编程_pygame(1)
  12. redis的几种数据导入导出方式【转】
  13. CentOS7 Zabbix3.4安装
  14. 微信为什么不能下载apk 微信不能打开App下载地址的问题
  15. FineCMS v5.4.1 后台getshell
  16. sql语句查询排序
  17. 1043 Is It a Binary Search Tree (25 分)
  18. skopt学习之路1-函数介绍:dummy_minimize
  19. BZOJ 2243 染色 | 树链剖分模板题进阶版
  20. mavern安装方法

热门文章

  1. OProfile 性能分析工具
  2. Sublime插件库新成员基于APICloud快速开发跨平台App
  3. iOS系统版本简介
  4. AbsListView.OnScrollListener
  5. C# WebBrowser控件 模拟登录 抓取数据
  6. shell中如何判断某一命令是否存在
  7. JMX超详细解读
  8. CSS位置如何获取的
  9. PHYLIP linux安装
  10. How to export a template in Visual Studio?