国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 邮箱| 柞水县| 乌拉特前旗| 金乡县| 武定县| 莱阳市| 延川县| 新昌县| 沁阳市| 瑞丽市| 陕西省| 廉江市| 镇安县| 肥乡县| 喀什市| 定边县| 边坝县| 大城县| 南漳县| 萝北县| 拉孜县| 乐安县| 临漳县| 金阳县| 兴海县| 太仓市| 堆龙德庆县| 临澧县| 乐至县| 区。| 汝城县| 弋阳县| 友谊县| 昌图县| 铜鼓县| 双峰县| 年辖:市辖区| 安岳县| 舞阳县| 郸城县| 巴南区|