SpringMVC定时器



图片说明版




文字说明版
  • 通过配置XML文件的方式来实现定时器功能
    • 在Spring-mvc.xml(ApplicationContext.xml)文件里面引入三个配置文件,不然使用<task:scheduled-tasks>标签会报错
      • xmlns:task="http://www.springframework.org/schema/task"
      • http://www.springframework.org/schema/task
      • http://www.springframework.org/schema/task/spring-task.xsd"
    • 在beans标签里面进行下面的配置
<!-- spring定时器 -->
<context:component-scan base-package="com.aflyun.web" />
<aop:aspectj-autoproxy proxy-target-class="true" />
<context:annotation-config />
<!-- 在applicationContext.xml中进行配置,使用定时器
ref : pojo类的名称
method : 调用的方式名称
cron : cronExpression表达式,想要查找自己所需要的时间格式可以在下面的这个网址里面进行转换
cron="0 0/1 0-2 * * ?" //表示每两小时执行一次
-->
<task:scheduled-tasks>
<task:scheduled ref="taskCool" method="testJob" cron="0 0/1 0-2 * * ?"/>
</task:scheduled-tasks>
  • 写实体类
package com.whx.mapper;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
//@Service 都可以
public class TaskCool {
@Autowired //注入mapper借口调用里面的方法
private *** ***;
/**
* 第一个定时器测试方法
*/
public void testJob(){
//调用定是修改数据库的方法
***.xxx();
Date date=new Date();
System.err.println("已调用定时检查状态方法 .... "+date);
}
}
  • 大功告成!可以测试一下看看效果o(* ̄︶ ̄*)o
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值