一、计划任务实现类

1、用@Component注解标识计划任务类,这样spring可以自动扫描

2、在方法中使用注解标识要执行的方法:@Scheduled(cron="*/30 * * * * *")

3、周期可以使用cron,或者fixedRate,fixedRate=1000*30表示30秒执行一次,cron请自行百度或看下面的代码的说明

@Component
public class SpringTask { /**
* cron表达式:* * * * * *(共6位,使用空格隔开,具体如下)
* cron表达式:*(秒0-59) *(分钟0-59) *(小时0-23) *(日期1-31) *(月份1-12或是JAN-DEC) *(星期1-7或是SUN-SAT)
* 注意: 30 * * * * * 表示每分钟的第30秒执行,而(*斜杠30)表示每30秒执行
*
* */
@Scheduled(cron="*/30 * * * * *")
public void firstTask(){
System.out.println("==============it is first task!时间:"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
}
}

二、需要在spring.xml配置文件中加入命名空间(xmlns:task)

本项目采用了spring4.1,所以用的是4.1

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">

三、增加包的扫描,一般在spring.xml文件都会有

主要是这个:<context:component-scan base-package="com.spring.*"></context:component-scan>

四、在spring.xml文件中配置计划任务

也可以简单点配置,如下:

<task:annotation-driven />    

 五、然后启动web项目,就会看到每30秒就打印信息出来。

六、如果需要用到service类,可以注入。

@Autowired
private PushRecordService pushRecordService;

转自:http://fanshuyao.iteye.com/blog/2267243

最新文章

  1. Notepad++使用小结
  2. Haskell 参考资料
  3. 01shell入门基础
  4. js 模块化历程
  5. java.util.List接口的方法subList()的使用注意事项
  6. linux内存回收 内核参数
  7. 数据结构与算法实验题7.1 M 商人的求救
  8. CSS Questions:Front-end Developer Interview Questions
  9. http协议分析工具【转】
  10. ZOJ 1654 - Place the Robots (二分图最大匹配)
  11. Flume与Kafka集成
  12. 从python的yield说起
  13. ubuntu下mysql的环境搭建及使用
  14. 函数可重入问题reentrant functions(函数执行过程中可以被中断,允许多个副本)
  15. phpcms v9 get调用标签整理
  16. 布局动画 LayoutAnimation
  17. Qt5:Qt文件操作类 QFile
  18. SQL Server-聚焦NOLOCK、UPDLOCK、HOLDLOCK、READPAST你弄懂多少?(三十四)
  19. 配置sonarqube与gitlab sso认证集成
  20. scala判断数据类型

热门文章

  1. 【黑金原创教程】【FPGA那些事儿-驱动篇I 】原创教程连载导读【连载完成,共二十九章】
  2. jenkins之 pipeline 小尝试
  3. 实现类似于QQ空间相册的点击图片放大,再点后缩小回原来位置
  4. java封装实现Excel建表读写操作
  5. 修改Jenkins的主目录步骤
  6. 180425、cookie工具类
  7. com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column &#39;id&#39; at row 1
  8. Sql Server :SELECT a.*,b.HZXM FROM YG_LIS_JCBYTK a(nolock)中的NOLOCK作用
  9. 【libreOJ模板】并查集(输入挂,取模与find优化)
  10. Exception 07 : org.hibernate.LazyInitializationException: could not initialize proxy - no Session