①:配置pom.xml
<dependency>
? ? ? ? ? ? <groupId>org.quartz-scheduler</groupId>
? ? ? ? ? ? <artifactId>quartz</artifactId>
? ? ? ? ? ? <version>2.2.3</version>
? ? ? ? </dependency>
②:spring核心配置文件 applicationContext.xml加入配置
頭部加入:
xmlns:task="http://www.springframework.org/schema/task"
xsi:http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task.xsd
具體配置加入:
<task:annotation-driven/>
③:在需要定時的方法上加入@Scheduled
@Scheduled(cron = "5 5/30 * * * *")//從第五分鐘開始攒至,每三十分鐘的第五秒執(zhí)行一次
public void quartzJobTestMethod() {
? ? ? ? System.out.println("定時任務執(zhí)行:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
? ? }
④:main方法
@EnableScheduling//開啟基于注解的定時任務
public class YaKiTimedTask {
private static ApplicationContext context;
public static void main(String[] args) {
context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
context.getBean(OrderUtils.class);//注入定時任務所在的類
}
}