1.创建springapplication-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-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.1.xsd">
<!--任务调度器-->
<task:scheduler id="taskScheduler" pool-size="1" />
<!---->
<task:annotation-driven executor="taskScheduler" proxy-target-class="true" />
<!--<task:scheduled-tasks scheduler="taskScheduler">
<task:scheduled ref="boxDeptController" method="runfirst" cron="0/5 * * * * ?"/>
</task:scheduled-tasks>-->
</beans>
2.在application.xml中加入
<import resource="application-task.xml"/>
3.测试
package com.efiles.test;
import org.springframework.scheduling.annotation.Scheduled;
/**
* @ClassName TaskTest
* @Author Administrator
* @Date 2020/10/13 0013 下午 1:55
**/
public class TaskTest {
/*5s执行一次*/
@Scheduled(cron = "0/5 * * * * ?")
public void runfirst(){
System.out.println("********first job is ok******" +System.currentTimeMillis());
}
}