国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

spring task 定時任務(wù)實現(xiàn)示例

2019-11-26 13:13:26
字體:
供稿:網(wǎng)友

一、引入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>

四、編寫實體類

package com.test.task;import java.text.DateFormat;import java.util.Date;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Component;@Componentpublic class TestTask {  @Scheduled(cron = "*/5 * * * * ?")  public void print(){    String time = DateFormat.getDateTimeInstance().format(new Date());    System.out.println("定時器觸發(fā)打印"+time);  }}

五、工程目錄:

運行結(jié)果:

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 宾川县| 阜阳市| 新密市| 壶关县| 广汉市| 洪江市| 宁津县| 彰武县| 瓮安县| 正蓝旗| 神农架林区| 海丰县| 兴安盟| 呼伦贝尔市| 蛟河市| 南阳市| 永修县| 阿拉善右旗| 周至县| 平南县| 梁平县| 聂拉木县| 开江县| 荥阳市| 兴隆县| 青州市| 博湖县| 崇仁县| 贺兰县| 云浮市| 靖安县| 郧西县| 江安县| 阳谷县| 呼和浩特市| 鄂托克前旗| 三原县| 鄂伦春自治旗| 车致| 济源市| 东莞市|