Spring 定时器

Spring 定时器 实例如下

第一步:

//首先创建一个任务类

 

package com.shangde.timer;

import java.sql.Time;
import java.util.Timer;
import java.util.TimerTask;

/*
 * 创建一个任务类
 */
public class Clock extends TimerTask{
 
 
 @Override
 public void run() {
  System.out.println("Hello Word");
 }

}
第二步

//创建quartzClock类并集成

 

package com.shangde.timer;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

public class quartzClock extends QuartzJobBean {

 @Override
 protected void executeInternal(JobExecutionContext arg0)
   throws JobExecutionException {
   System.out.println("这里执行定时器的任务");
   System.out.println("Quartz.........");
 }

}

第三步:

//在src下创建一个ApplicationContext.xml(配置如下)

 

<?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:aop="http://www.springframework.org/schema/aop"
 xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
  http://www.springframework.org/schema/context  
   http://www.springframework.org/schema/context/spring-context-2.5.xsd
">
<!-- spring定时器 -->  
  
<!-- 第一步 声明一个定时任务,该类extends java.util.TimerTask -->  
<!-- 就是把自己的类跟方法定义在配置文件中就可以了
 然后他会根据你的时间来调用你的类里面的方法
            达到定时的效果
 -->
 <bean id="mesbean" class="com.shangde.timer.SpringTest" abstract="false"
  lazy-init="default" autowire="default" dependency-check="default">
 </bean>
 
   <bean id="Clock" class="com.shangde.timer.Clock"></bean>
  
<!-- 第二步 调度定时任务,把声明的定时任务注入进来,并设置定时参数 -->  
<bean id="scheduledClock" class="org.springframework.scheduling.timer.ScheduledTimerTask">  
   <property name="timerTask">  
    <ref bean="Clock"></ref>  
   </property>  
   <property name="period">  
    <value>1000</value>  
    <!--这里是每隔多长时间就进行一次计时任务,单位ms-->  
   </property>  
   <property name="delay">  
    <value>1000</value>  
    <!--这里是服务启动后延时多少时间,开始计时任务,单位ms-->  
   </property>  
</bean>  
  
<!-- 启动定时任务,如果有多个定时任务,则重复步骤一,二,然后把第二步设置的beany放在下面的list列表中.此方法不能精确几点运行定时任务 -->  
<bean class="org.springframework.scheduling.timer.TimerFactoryBean">  
   <property name="scheduledTimerTasks">  
    <list>  
     <ref bean="scheduledClock"></ref>  
    </list>  
   </property>  
</bean>  
</beans>

 

//第四步

//创建一个测试类

 

package com.shangde.timer;

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringTest {
 public static Logger logg=Logger.getLogger(SpringTest.class);
 
 
 public static void main(String[] args) {
//  第一种写法加载配置文件
  ApplicationContext  applicationContext=new ClassPathXmlApplicationContext("ApplicationContext.xml");
  
  
//  第二种写法
  /*ClassPathXmlApplicationContext ct=new ClassPathXmlApplicationContext("/ApplicationContext.xml");
  ApplicationContext aContext=new ClassPathXmlApplicationContext("ApplicationContext.xml");*/
//  加载多个配置文件
//  ApplicationContext ctContext=new ClassPathXmlApplicationContext(new String[]{"ApplicationContext.xml","ApplicationContext.xml"});
  
 }
}
//最后输出结果如下(我一步步测试完成的,如需要可以直接复制)

log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
Hello Word
Hello Word
Hello Word
Hello Word
Hello Word
Hello Word
Hello Word
Hello Word
Hello Word
Hello Word

有疑问可以留言!!!

 

你可能感兴趣的:(Spring 定时器 实例)