SpringMVC定时器



图片说明版
SpringMVC定时器

SpringMVC定时器

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