一、首先配置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秒执行一次任务!");
}
}

效果图:

Spring注解@Scheduled定时任务的更多相关文章

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

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

  2. spring注解@Scheduled中fixedDelay、fixedRate和cron表达式的区别

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  3. Spring Boot Scheduled定时任务特性

    SpringBoot中的Scheduled定时任务是Spring Boot中非常常用的特性,用来执行一些比如日切或者日终对账这种定时任务 下面说说使用时要注意的Scheduled的几个特性 Sched ...

  4. spring的@Scheduled定时任务,同一时间段的定时任务只会执行一个,其余的会被阻塞,@Scheduled注解定时任务并发执行解决办法,即多线程运行定时任务

    原文:https://blog.csdn.net/qq_35937303/article/details/88851064 现有两个定时任务 @Component("aa") pu ...

  5. Spring 注解 @Scheduled(cron = "0 0/10 * * * ? ") 任务调度动态改变时间

    不需要重启应用就可以动态的改变Cron表达式的值 import java.util.Date; import java.util.concurrent.Executor; import java.ut ...

  6. Spring注解配置定时任务<task:annotation-driven/>

    http://m.blog.csdn.net/article/details?id=50945311 首先在配置文件头部的必须要有: xmlns:task="http://www.sprin ...

  7. spring注解 @Scheduled(cron = "0 0 1 * * *")实现定时的执行任务

    @Scheduled(cron = "0 0 1 * * *") 在使用该注解以前请做好以下准备工作,配置好相应的xm文件. 配置定时注解的步骤:http://blog.csdn. ...

  8. Spring 注解 @Scheduled(cron = "0 0/10 * * * ? ") 动态改变时间

    import java.util.Date; import java.util.concurrent.Executor; import java.util.concurrent.Executors; ...

  9. Spring 的@Scheduled注解实现定时任务运行和调度

    Spring 的@Scheduled注解实现定时任务运行和调度 首先要配置我们的spring.xml   ---  即spring的主配置文件(有的项目中叫做applicationContext.xm ...

随机推荐

  1. ELK-Python(一)

    不具有通用性,留作纪念. [root@GXB-CTRLCENTER python]# cat insert_active_user.py #!/usr/bin/env python # -*- cod ...

  2. 微信小店 API 手册

    微信商铺API手册V1.13 目录 1.      商品管理接口.................................................................... ...

  3. Jenkins定时构建项目

    我们是不是可以自动的来构建项目那么此时我们需要使用Poll SCM和Build periodically,我们在构建触发中选择这两项即可,其实他们两个就是一个自动任务 触发远程构建:触发远程构建Bui ...

  4. gcc选项-g与-rdynamic的异同_转

    转自:http://www.tuicool.com/articles/EvIzUn gcc 的 -g ,应该没有人不知道它是一个调试选项,因此在一般需要进行程序调试的场景下,我们都会加上该选项,并且根 ...

  5. MongoDB ObjectId

    概述 > db.col.find() { , } { , } { , } { , } 每个文档中都有一个“_id”,她是一个12字节的BSON类型数据,格式如下 56c56dd4ca446fab ...

  6. nodejs weixin 笔记

    http://www.oschina.net/code/snippet_218887_25870 好文章: http://codelife.me/blog/2013/04/23/developing- ...

  7. Python:安装mssql模块功能,并实现与sqlserver连接、查询

    由于我系统是x64系统,所以下载python2.7 x64.下载地址:https://www.python.org/downloads/release/python-2712/, 经过测试发现这个版本 ...

  8. NHibernate 3.2+的 Map by Code 实例

    NHibernate 3.2+的 Map by Code 的文档真的很少,除了Fabio Maulo的2篇blog(顺带说李永京的翻译,没任何新的研究). 让人感慨NH的没落, 也许从没有流行过. 在 ...

  9. 实验十五_安装新的int 9中断例程

    安装一个新的int 9中断例程,功能:在DOS下,按下“A”键后,除非不在松开,    如果松开,就显示满屏幕的“A”:其他的键照常处理. 提示:按下一个键时产生的扫描码称为通码,松开一个键产生的扫描 ...

  10. jsp eclipse 创建jsp项目

    选择File菜单 File->new->Other... 选择Dynamic Web Project项目->Next 写入项目->Finish 在WebContent鼠标右键- ...