spring xml 定时任务

启动服务器,根据时间设置规则,执行相应操作

想了解时间规则(click me)

一、写一个操作类

1.记住类路径


2.记住类名和方法名


二、修改xml文件

1. 创建一个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:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

	
</beans>
2. 将操作类配置进去(完整类路径名)

<bean id="myTimer" class="com.timer.MyTimer"></bean>
3. 定时器配置(类名和方法名)

<!-- 定时器配置 -->
	<bean id="timeDitail"
		class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
		<property name="targetObject" ref="myTimer"></property>
		<property name="targetMethod" value="showtime"></property>
	</bean>
4. 定义时间间隔触发器(每五秒触发一次)

<bean id="timeTigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
		<property name="jobDetail" ref="timeDitail"></property>
		<property name="cronExpression">
			<value>*/5 * * * * ?</value>
		</property>
	</bean>
5. 管理并启动触发器

<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
		<property name="triggers">
			<list>
				<ref bean="timeTigger" />
			</list>
		</property>
	</bean>


三、修改web.xml文件
添加如下内容

<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>

	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

四、导入jar包

1. spring

2.quartz

3.commons-logging和commons-collections

4.log4j和slf4j

五、启动tomcat,在Console窗口看到如下结果,并每五秒会有一条新纪录




完整项目下载:http://download.csdn.net/detail/flykos/9698965



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值