spring配置时钟具体注意细节和代码
本来很简单的时钟今天配的时候却弄了我2个小时,在这里跟大家分享下错误经验。希望能帮助大家少走弯路。
1,注意jar包,特别是commons-collections.jar这个包的版本要3.x以上的。
2,注意编译jdk和运行jdk的版本问题要不然会报:java.lang.UnsupportedClassVersionError: Bad version number in .class file ...错误
配置一个服务时钟的类源码:
1,ClockService.java:处理时钟业务
public class ClockService {public void doService(){System.out.println("do Service!");}}
public class ClockTime extends QuartzJobBean{protected void executeInternal(JobExecutionContext arg0)throws JobExecutionException {System.out.println("============== Time Clock Start =============");ClockService service = new ClockService();service.doService();System.out.println("============== Time Clock End =============");}}
<?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="quartzClock"/></property><property name="startDelay"><value>6000</value><!-- 这里是服务启动后延时多少时间,开始计时任务,单位ms--></property><property name="repeatInterval"><value>30000</value><!-- 这里是每隔多长时间就进行一次计时任务,单位ms --></property></bean><bean id="cronQuartzClock"name="code"><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>