spring task 定時(shí)任務(wù)實(shí)現(xiàn)示例
一、引入spring相關(guān)jar包:

二、在web.xml中配置spring
<listener> <description>Spring監(jiān)聽器</description> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param>
三、在applicationContext.xml中配置監(jiān)聽器
<?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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd"
default-lazy-init="false">
<!-- 注解方式 -->
<context:annotation-config />
<context:component-scan base-package="com.test.task" />
<task:annotation-driven/>
<!-- XML方式 -->
<!-- <bean name="testTask" class="com.test.task.TestTask" lazy-init="false"></bean>
<task:scheduled-tasks>
<task:scheduled ref="testTask" method="print" cron="0/5 * * * * ?"/>
</task:scheduled-tasks> -->
</beans>
四、編寫實(shí)體類
package com.test.task;
import java.text.DateFormat;
import java.util.Date;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class TestTask {
@Scheduled(cron = "*/5 * * * * ?")
public void print(){
String time = DateFormat.getDateTimeInstance().format(new Date());
System.out.println("定時(shí)器觸發(fā)打印"+time);
}
}
五、工程目錄:

運(yùn)行結(jié)果:

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 使用spring-task定時(shí)任務(wù)動(dòng)態(tài)配置修改執(zhí)行時(shí)間
- Spring Task定時(shí)任務(wù)每天零點(diǎn)執(zhí)行一次的操作
- SpringBoot整合SpringTask實(shí)現(xiàn)定時(shí)任務(wù)的流程
- mall整合SpringTask實(shí)現(xiàn)定時(shí)任務(wù)的方法示例
- java 中Spring task定時(shí)任務(wù)的深入理解
- Spring Task定時(shí)任務(wù)的配置和使用詳解
- Spring整合TimerTask實(shí)現(xiàn)定時(shí)任務(wù)調(diào)度
- Spring Task定時(shí)任務(wù)的實(shí)現(xiàn)詳解
相關(guān)文章
java實(shí)現(xiàn)的統(tǒng)計(jì)字符算法示例
這篇文章主要介紹了java實(shí)現(xiàn)的統(tǒng)計(jì)字符算法,涉及java針對(duì)字符的遍歷、判斷、運(yùn)算等相關(guān)操作技巧,需要的朋友可以參考下2017-10-10
智能 AI 代碼生成工具 Cursor 安裝和使用超詳細(xì)教程
詳解Java利用ExecutorService實(shí)現(xiàn)同步執(zhí)行大量線程
SpringBoot中的application.properties無法加載問題定位技巧
Spring Boot中進(jìn)行 文件上傳和 文件下載功能實(shí)現(xiàn)
Java OpenCV利用KNN算法實(shí)現(xiàn)圖像背景移除

