启动服务器,根据时间设置规则,执行相应操作
一、写一个操作类
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>
添加如下内容
<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