在Spring3.0以上版本中,利用配置文件实现定时任务
2、在spring配置文件applicationContext-service.xml中添加命名空间和描述
1、在service.java中编写需要定时执行的任务的方法;
//定时任务
public void springTask(){
System.out.println("定时任务");
deleteByuploadAuto();
}
2、在spring配置文件applicationContext-service.xml中添加命名空间和描述
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:task="httpk://www.springframework.org/schema/task"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.3.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.1.xsd
http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring
http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd">
3、在spring配置文件applicationContext-service.xml中添加任务
<task:annotation-driven /> <!-- 定时器开关-->
<task:scheduled-tasks>
<task:scheduled ref="storageService" method="job1" cron="0/5 * * * * ?"/> <!-- 此处设置5秒执行一次-->
</task:scheduled-tasks>
4、启动tomcat。