欢迎使用优快云-markdown编辑器

本文介绍如何在SpringMVC项目中实现定时任务,通过配置XML文件和使用注解的方式,详细展示了定时任务的实现过程及一个具体的测试类示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近在做springmvc的项目,其中用到了定时任务,发上来,大家一起学习下。

在spring-mvc.xml文件中加入

1.xmlns:task=”http://www.springframework.org/schema/task

2.xsi:schemaLocation中加入

http://www.springframework.org/schema/task

     http://www.springframework.org/schema/task/spring-task-4.1.xsd

3.添加任务扫描注解

4.添加注解包

提供一个测试类

package com.ivox.wsms.quartz;

import java.text.DateFormat;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.scheduling.annotation.Scheduled;

import org.springframework.stereotype.Component;

import com.ivox.wsms.entity.MessageInfo;

import com.ivox.wsms.service.MessageInfoService;

@Component

public class MyTask {

@Autowired

private MessageInfoService messageService;

@Scheduled(cron="0/10 * * * * ? ") //间隔5秒执行

public void taskCycle(){

    DateFormat format=new SimpleDateFormat("yyyy-MM-dd");

    List infos = messageService.queryMessageInfoByStartDate(format.format(new Date()));

    System.out.println(infos.size());

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值