A .pom.xml
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.20.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.3.20.RELEASE</version> </dependency>
Mandate class
public class TaskService { public void firstTask(){ SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm:ss:ms"); String format = simpleDateFormat.format(new Date()); System.out.println("数据库备份时间1:"+format); } public void secondTask(){ SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm:ss:ms"); String format = simpleDateFormat.format(new Date()); System.out.println("数据库备份时间2:"+format); } }
三.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:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd"> <bean id="taskService" class="com.wj.TaskService"> </bean> <!--配置定时规则--> <task:scheduled-tasks> <- initial-delay:! how many milliseconds after the server is started to perform regular tasks start -> <- fixed-delay:! executed once every how many milliseconds regular tasks -> : <! - - You can specify the timing of complex tasks, have Quartz hereinbefore described, will not be described the cron> <Task: Scheduled REF = "TaskService "Method =" firstTask "Initial-Delay =" 10000 "Fixed-Delay =" 2000 "/> <Task: Scheduled REF =" TaskService "Method =" SecondTask "Initial-Delay =" 20000 "Fixed-Delay =" 4000 " /> </ Task: Scheduled-Tasks> </ Beans>
Four operating results: