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"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
- <bean id="mesBean" class="cn.xg.spring.Message" abstract="false"
- lazy-init="default" autowire="default" dependency-check="default">
- <property name="title">
- <value>标题</value>
- </property>
- </bean>
- <!-- spring定时器 -->
- <!-- 方法一 -->
- <!-- 第一步 声明一个定时任务,该类extends java.util.TimerTask -->
- <bean id="clock" class="cn.xg.spring.Clock"></bean>
- <!-- 第二步 调度定时任务,把声明的定时任务注入进来,并设置定时参数 -->
- <bean id="scheduledClock" class="org.springframework.scheduling.timer.ScheduledTimerTask">
- <property name="timerTask">
- <ref bean="clock"></ref>
- </property>
- <property name="period">
- <value>5000</value>
- <!--这里是每隔多长时间就进行一次计时任务,单位ms-->
- </property>
- <property name="delay">
- <value>5000</value>
- <!--这里是服务启动后延时多少时间,开始计时任务,单位ms-->
- </property>
- </bean>
- <!-- 启动定时任务,如果有多个定时任务,则重复步骤一,二,然后把第二步设置的beany放在下面的list列表中.此方法不能精确几点运行定时任务 -->
- <bean class="org.springframework.scheduling.timer.TimerFactoryBean">
- <property name="scheduledTimerTasks">
- <list>
- <ref bean="scheduledClock"></ref>
- </list>
- </property>
- </bean>
- <!-- 方法二 -->
- <!-- 第一步 声明一个定时任务,注意不是直接声明,而是声明一个JobDetailBean,通过jobClass属性设置一个定时对象 -->
- <bean id="quartzClock" class="org.springframework.scheduling.quartz.JobDetailBean">
- <property name="jobClass">
- <value>cn.xg.spring.QuartzClock</value>
- </property>
- </bean>
- <!-- 第二步 调度定时任务 -->
- <!--这种配置与第一种方法效果一样
- <bean id="quartzClockTask" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
- <property name="jobDetail">
- <ref bean="quartzClock"/>
- </property>
- <property name="startDelay">
- <value>6000</value>
- 这里是服务启动后延时多少时间,开始计时任务,单位ms
- </property>
- <property name="repeatInterval">
- <value>6000</value>
- 这里是每隔多长时间就进行一次计时任务,单位ms
- </property>
- </bean>
- -->
- <!-- 这种配置可以精确几点执行定时任务 -->
- <bean id="cronQuartzClock" class="org.springframework.scheduling.quartz.CronTriggerBean">
- <property name="jobDetail">
- <ref bean="quartzClock"></ref>
- </property>
- <property name="cronExpression">
- <value>0 52 22 * * ?</value><!--定时在任何月份任何日期(不管星期几)的22点52分0秒 -->
- <!-- 一个cron表达式有到少6个(也可能是7个)由空格分隔的时间元素.从左到右,这些元素的定义如下:
- 1.秒(0-59)
- 2.分钟(0-59)
- 3.小时(0-23)
- 4.月份中的是期(1-31)
- 5.月份(1-12或SUN-DEC)
- 6.星期中的日期(1-7或SUN-SAT)
- 7.年份(1970-2099)
- 例子:
- 0 0 10,14,16 * * ? 每天上午10点,下午2点和下午4点
- 0 0,15,30,45 * 1-10 * ? 每月前10天每隔15分钟
- 30 0 0 1 1 ? 2012 在2012年1月1日午夜过30秒时
- 0 0 8-5 ? * MON-FRI 每个工作日的工作时间
- - 区间
- * 通配符
- ? 你不想设置那个字段
- -->
- </property>
- </bean>
- <!--第三步 启动定时任务,注意这里的ref bean -->
- <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
- <property name="triggers">
- <list>
- <ref bean="cronQuartzClock"></ref>
- </list>
- </property>
- </bean>
- </beans>
clock.java
- import java.util.TimerTask;
- public class Clock extends TimerTask{
- @Override
- public void run() {
- System.out.println("clock..!clock....!.......");
- }
- }
import java.util.TimerTask;
public class Clock extends TimerTask{
@Override
public void run() {
System.out.println("clock..!clock....!.......");
}
}
QuartzClock .java
- 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("QuartzClock..!QuartzClock....!.......");
- }
- }
- SpringTest .java
- package cn.xg.spring;
- import org.apache.log4j.Logger;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class SpringTest {
- /**
- * @param args
- */
- public static Logger log = Logger.getLogger(SpringTest.class);
- public static void main(String[] args) {
- //第一种写法(加载配置文件)
- ApplicationContext ctx = new
- ClassPathXmlApplicationContext("applicationContext.xml");
- //第二种写法
- //ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");
- //ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
- //加载多个配置文件
- // ApplicationContext ctx = new ClassPathXmlApplicationContext(
- //new String[]{"applicationContext.xml","applicationContext2.xml"} );
- }
- }
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("QuartzClock..!QuartzClock....!.......");
}
}
SpringTest .java
package cn.xg.spring;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringTest {
/**
* @param args
*/
public static Logger log = Logger.getLogger(SpringTest.class);
public static void main(String[] args) {
//第一种写法(加载配置文件)
ApplicationContext ctx = new
ClassPathXmlApplicationContext("applicationContext.xml");
//第二种写法
//ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");
//ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//加载多个配置文件
// ApplicationContext ctx = new ClassPathXmlApplicationContext(
//new String[]{"applicationContext.xml","applicationContext2.xml"} );
}
}
所需要的jar包:
spring.jar;quartz-all-1.6.0.jar;commons-collections.jar;可能还要commons-*.jar