spring中的定时任务
- 格式:rtf
- 大小:17.44 KB
- 文档页数:8
spring定时任务详解(@Scheduled注解)Spring配置⽂件xmlns加⼊xmlns:task="/schema/task"xsi:schemaLocation中加⼊/schema/task/schema/task/spring-task-3.0.xsd"Spring扫描注解的配置<context:component-scan base-package="com.imwoniu.*" />任务扫描注解<task:executor id="executor" pool-size="5" /><task:scheduler id="scheduler" pool-size="10" /><task:annotation-driven executor="executor" scheduler="scheduler" />代码实现:注解@Scheduled 可以作为⼀个触发源添加到⼀个⽅法中,例如,以下的⽅法将以⼀个固定延迟时间5秒钟调⽤⼀次执⾏,这个周期是以上⼀个调⽤任务的完成时间为基准,在上⼀个任务完成之后,5s后再次执⾏:@Scheduled(fixedDelay = 5000)public void doSomething() {// something that should execute periodically}如果需要以固定速率执⾏,只要将注解中指定的属性名称改成fixedRate即可,以下⽅法将以⼀个固定速率5s来调⽤⼀次执⾏,这个周期是以上⼀个任务开始时间为基准,从上⼀任务开始执⾏后5s再次调⽤:@Scheduled(fixedRate = 5000)public void doSomething() {// something that should execute periodically}如果简单的定期调度不能满⾜,那么cron表达式提供了可能package com.imwoniu.task;import org.springframework.scheduling.annotation.Scheduled;import ponent;@Componentpublic class TaskDemo {@Scheduled(cron = "0 0 2 * * ?") //每天凌晨两点执⾏void doSomethingWith(){("定时任务开始......");long begin = System.currentTimeMillis();//执⾏数据库操作了哦...long end = System.currentTimeMillis();("定时任务结束,共耗时:[" + (end-begin) / 1000 + "]秒");}}关于Cron表达式(转载)表达式⽹站⽣成:按顺序依次为秒(0~59)分钟(0~59)⼩时(0~23)天(⽉)(0~31,但是你需要考虑你⽉的天数)⽉(0~11)天(星期)(1~7 1=SUN 或 SUN,MON,TUE,WED,THU,FRI,SAT)7.年份(1970-2099)其中每个元素可以是⼀个值(如6),⼀个连续区间(9-12),⼀个间隔时间(8-18/4)(/表⽰每隔4⼩时),⼀个列表(1,3,5),通配符。
SpringScheduled定时任务+异步执⾏Spring定时任务 + 异步执⾏1.实现spring定时任务的执⾏import org.springframework.scheduling.annotation.Scheduled;import ponent;import java.text.SimpleDateFormat;import java.util.Date;@Componentpublic class ScheduledTasks {private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");@Scheduled(fixedDelay = 5000)public void first() throws InterruptedException {System.out.println("第⼀个定时任务开始 : " + sdf.format(new Date()) + "\r\n线程 : " + Thread.currentThread().getName());System.out.println();Thread.sleep(1000 * 10); //模拟第⼀个任务执⾏的时间}@Scheduled(fixedDelay =3000)public void second() {System.out.println("第⼆个定时任务开始 : " + sdf.format(new Date()) + "\r\n线程 : " + Thread.currentThread().getName());System.out.println();}}由于执⾏定时任务默认的是单线程,存在的问题是:线程资源被第⼀个定时任务占⽤,第⼆个定时任务并不是3秒执⾏⼀次;解决⽅法,创建定时任务的线程调度器,交给spring管理调度import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.scheduling.TaskScheduler;import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;@Configurationpublic class ScheduleConfig {@Beanpublic TaskScheduler taskScheduler() {ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();scheduler.setPoolSize(10);return scheduler;}}这样就可以保证定时任务之间是多线程,⽽每个定时任务之间是单线程,保证了定时任务的异步,同时保证了消息的幂等性。
Spring提供的三种定时任务机制及其比较定时任务的需求在众多应用系统中广泛存在,在Spring中,我们可以使用三种不同的定时机制,下面一一描述并加以比较1. 基于Quartz的定时机制下面详细解释这个类图中涉及的关键类及其使用场景1.1. SchedulerFactoryBean这是Spring中基于Quartz的定时机制入口,只要Spring容器装载了这个类,Quartz定时机制就会启动,并加载定义在这个类中的所有triggerSpring配置范例:[xhtml]view plaincopy1.<bean id="sfb"class="org.springframework.scheduling.quartz.SchedulerFactoryBean">2.<!-- 添加触发器 -->3.<property name="triggers">4.<list>5.<ref local="appSubscTrigger"/>6.</list>7.</property>8.9.<!-- 添加listener -->10.<property name="globalTriggerListeners">11.<list>12.<ref local="myTaskTriggerListener"/>13.</list>14.</property>15.</bean>1.2. CronTriggerBean实现了Trigger接口,基于Cron表达式的触发器这种触发器的好处是表达式与linux下的crontab一致,能够满足非常复杂的定时需求,也容易配置Spring配置范例:[xhtml]view plaincopy1.<bean id="notifyTrigger"class="org.springframework.scheduling.quartz.CronTriggerBean">2.<property name="jobDetail"ref="notifyJobDetail"/>3.<property name="cronExpression"value="${notify_trigger_cron_expression}"/>4.</bean>1.3. SimpleTriggerBean该类也实现了Trigger接口,基于配置的定时调度这个触发器的优点在于很容易配置一个简单的定时调度策略Spring配置范例:[xhtml]view plaincopy1.<bean id="simpleReportTrigger"class="org.springframework.scheduling.quartz.SimpleTriggerBean">2.<property name="jobDetail">3.<ref bean="reportJob"/>4.</property>5.<property name="startDelay">6.<value>3600000</value>7.</property>8.<property name="repeatInterval">9.<value>86400000</value>10.</property>11.</bean>1.4. JobDetailBeanJobDetail类的简单扩展,能够包装一个继承自QuartzJobBean的普通Bean,使之成为定时运行的Job缺点是包装的Bean必须继承自一个指定的类,通用性不强,对普通Job的侵入性过强,不推荐使用1.5. MethodInvokingJobDetailFactoryBeanSpring提供的一个不错的JobDetail包装工具,能够包装任何bean,并执行类中指定的任何stati或非static的方法,避免强制要求bean去实现某接口或继承某基础类Spring配置范例:[xhtml]view plaincopy1.<bean id="notifyJobDetail"parent="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">2.<property name="targetObject"ref="notifyServerHandler"/>3.<property name="targetMethod"value="execute"/>4.</bean>1.6. 关于TriggerListener和JobListenerQuartz中提供了类似WebWork的拦截器的功能,系统执行任务前或任务执行完毕后,都会检查是否有对应的Listener需要被执行,这种AOP的思想为我们带来了灵活的业务需求实现方式。
spring定时器定时任务到时间未执⾏问题的解决⽬录spring定时器定时任务到时间未执⾏应⽤场景原因分析解决⽅式解决修改系统时间后Spring 定时任务不执⾏问题描述起因错误解决问题spring定时器定时任务到时间未执⾏应⽤场景⼀个定时器类中有n个定时任务,有每30秒执⾏⼀次的还有每1分钟执⾏⼀次的,出现问题的定时任务是0点整时执⾏的定时任务到了0点没有执⾏。
原因分析spring定时器任务scheduled-tasks默认配置是单线程串⾏执⾏的,当某个定时任务出现阻塞,或者执⾏时间过长,则线程就会被占⽤,其他定时任务排队执⾏,导致后⾯的定时任务未能准时执⾏。
解决⽅式开启多线程定时任务执⾏/*** 多线程执⾏定时任务*/@Configurablepublic class ScheduleConfig implements SchedulingConfigurer {private static final int FIVE = 5;@Overridepublic void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {scheduledTaskRegistrar.setScheduler(Executors.newScheduledThreadPool(FIVE));}}解决修改系统时间后Spring 定时任务不执⾏问题描述Spring 定时任务不执⾏事情起因是这样的,我们有⼀个spring定时任务,每隔半⼩时要执⾏⼀次。
起因由于种种原因,昨晚上这台服务器被关机了,今早【重启服务器】和【启动定时任务服务】。
机器重启后,发现服务器机器系统时间和实际北京时间不⼀致,相差10个⼩时。
于是乎,我使⽤date -s 10:35:35 设置和北京时间保持⼀致。
错误本以为这样,时间已经⼀致了,定时任务应该能正常执⾏了!等了好⼏个⼩时,定时任务依然没有执⾏。
如何在spring中配置定时任务(job)-zxs9999的专栏-CSDN博客如何在spring中配置定时任务(job) 收藏一、在web.xml文件中进行如下配置:<context-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/classes/applicationContext-*.xml</param-value></context-param>那么需要在工程下创建一个以applicationContext- 为开头的xml 文件eg:applicationContext-jobconfig.xmlxml的头和结尾部分跟其他spring配置文件相似,就不赘述,正文如下:<bean id="youJobName(类别名)" class="com.******.YourJobClassLocation(类的定位)" /> <bean id="doYourJob(别名)" class="org.springframework.scheduling.quartz.MethodInvoking JobDetailFactoryBean"><property name="targetObject"><ref bean="youJobName(类别名)""/></property><property name="targetMethod"><value>runMethodName(定时执行的方法名)</value></property></bean><bean id="youJobNameTrigger(触发器别名)" class="org.springframework.scheduling.quartz.CronTriggerBean"><property name="jobDetail"><ref bean="doYourJob(别名)""/></property><property name="cronExpression"><value>0 0/20 * * * ?(定时的时间配置)</value></property></bean><bean id="doScheduler" class="org.springframework.scheduling.quartz.SchedulerFactory Bean"><property name="triggers"><list><ref local="youJobNameTrigger(触发器别名)"/></list></property></bean>这样的配置几本就可以运转了,但是有一个地方可能是你需要根据你的需求来确定的,那就是触发时间。
定时任务配置说明第一步:Spring-context.xml中配置<beans xmlns="/schema/beans"......xmlns:task="/schema/task"xsi:schemaLocation="....../schema/task/schema/task/spring-task-3.0.xsd"><task:executor id="executor" pool-size="5" /><task:scheduler id="scheduler" pool-size="5" /><task:annotation-driven executor="executor" scheduler="scheduler" />第二步:类名上面加@Service@Lazy(false)如图:第三步:方法名前加@Scheduled(cron = "0/5 * * * * ?")时间设置:// */5 * * * * ? 每隔5秒执行一次// 0 */1 * * * ? 每隔1分钟执行一次// 0 0 5-15 * * ? 每天5-15点整点触发// 0 0/3 * * * ? 每三分钟触发一次// 0 0-5 14 * * ? 在每天下午2点到下午2:05期间的每1分钟触发// 0 0/5 14 * * ? 在每天下午2点到下午2:55期间的每5分钟触发// 0 0/5 14,18 * * ? 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发// 0 0/30 9-17 * * ? 朝九晚五工作时间内每半小时// 0 0 10,14,16 * * ? 每天上午10点,下午2点,4点//// 0 0 12 ? * WED 表示每个星期三中午12点// 0 0 17 ? * TUES,THUR,SAT 每周二、四、六下午五点// 0 10,44 14 ? 3 WED 每年三月的星期三的下午2:10和2:44触发// 0 15 10 ? * MON-FRI 周一至周五的上午10:15触发// 0 0 23 L * ? 每月最后一天23点执行一次// 0 15 10 L * ? 每月最后一日的上午10:15触发// 0 15 10 ? * 6L 每月的最后一个星期五上午10:15触发// 0 15 10 * * ? 2005 2005年的每天上午10:15触发// 0 15 10 ? * 6L 2002-2005 2002年至2005年的每月的最后一个星期五上午10:15触发// 0 15 10 ? * 6#3 每月的第三个星期五上午10:15触发////// "30 * * * * ?" 每半分钟触发任务// "30 10 * * * ?" 每小时的10分30秒触发任务// "30 10 1 * * ?" 每天1点10分30秒触发任务// "30 10 1 20 * ?" 每月20号1点10分30秒触发任务// "30 10 1 20 10 ? *" 每年10月20号1点10分30秒触发任务// "30 10 1 20 10 ? 2011" 2011年10月20号1点10分30秒触发任务// "30 10 1 ? 10 * 2011" 2011年10月每天1点10分30秒触发任务// "30 10 1 ? 10 SUN 2011" 2011年10月每周日1点10分30秒触发任务// "15,30,45 * * * * ?" 每15秒,30秒,45秒时触发任务// "15-45 * * * * ?" 15到45秒内,每秒都触发任务// "15/5 * * * * ?" 每分钟的每15秒开始触发,每隔5秒触发一次// "15-30/5 * * * * ?" 每分钟的15秒到30秒之间开始触发,每隔5秒触发一次// "0 0/3 * * * ?" 每小时的第0分0秒开始,每三分钟触发一次// "0 15 10 ? * MON-FRI" 星期一到星期五的10点15分0秒触发任务// "0 15 10 L * ?" 每个月最后一天的10点15分0秒触发任务// "0 15 10 LW * ?" 每个月最后一个工作日的10点15分0秒触发任务// "0 15 10 ? * 5L" 每个月最后一个星期四的10点15分0秒触发任务// "0 15 10 ? * 5#3" 每个月第三周的星期四的10点15分0秒触发任务。
SpringTask定时任务配置与使⽤
1、cron表达式格式
{秒} {分} {时} {⽇} {⽉} {周} {年(可选)}
2、cron各位置取值符
"*" 代表每隔1分/秒/时触发;
"," 代表在指定的分/秒/时触发,⽐如"10,20,40"代表10分/秒/时、20分/秒/时和40分/秒/时时触发任务
"-" 代表在指定的范围内触发,⽐如"5-30"代表从5分/秒/时开始触发到30分/秒/时结束触发,每隔1分/秒/时触发
"/" 代表触发步进(step),"/"前⾯的值代表初始值("*"等同"0"),后⾯的值代表偏移量,⽐如"0/25"或者"*/25"代表从0分/秒/时开始,每隔25分/秒/时触发1次,即0分/秒/时触发1次,第25分/秒/时触发1次,第50分/秒/时触发1次;"5/25"代表5分/秒/时触发1参考博客:
(1)
(2)。
Spring@Scheduled定时任务的fixedRate,fixedDelay,cro。
⼀. 三种定时类型。
1.cron --@Scheduled(cron="0/5 * * * *?")当时间达到设置的时间会触发事件。
上⾯那个例⼦会每5秒执⾏⼀次。
2018/1/4 14:27:302018/1/4 14:27:352018/1/4 14:27:402018/1/4 14:27:452018/1/4 14:27:502.fixedRate --@Scheduled(fixedRate=2000)每两秒执⾏⼀次时间。
3.fixedDelay --@Scheduled(fixedDelay=2000)每次任务执⾏完之后的2s后继续执⾏看字⾯意思容易理解,但是任务执⾏长度超过周期会怎样呢?不多说,直接上图:import java.text.DateFormat;import java.text.SimpleDateFormat;import java.util.Date;import org.springframework.scheduling.annotation.Scheduled;import ponent;@Componentpublic class MyProcessor{DateFormat sdf = new SimpleDateFormat("HH:mm:ss");int[] delays = new int[]{8,3,6,2,2};int index = 0;@Scheduled(cron = "0/5 * * * * ?}")public void process() {try {if(index > delays.length - 1){if(index == delays.length){System.out.println("---------- test end at " + sdf.format(new Date()) + " ---------");}index ++;return;}else{System.out.println(index + ":start run at" + sdf.format(new Date()));}Thread.sleep(delays[index] * 1000);System.out.println(index + ":end run at " + sdf.format(new Date()));index ++;} catch (InterruptedException e) {e.printStackTrace();}}}。
Spring中的线程池和定时任务功能1.功能介绍Spring框架提供了线程池和定时任务执⾏的抽象接⼝:TaskExecutor和TaskScheduler来⽀持异步执⾏任务和定时执⾏任务功能。
同时使⽤框架⾃⼰定义的抽象接⼝来屏蔽掉底层JDK版本间以及Java EE中的线程池和定时任务处理的差异。
另外Spring还⽀持集成JDK内部的定时器Timer和Quartz Scheduler框架。
2.线程池的抽象:TaskExecutorTaskExecutor涉及到的相关类图如下:TaskExecutor接⼝源代码如下所⽰:public interface TaskExecutor extends Executor {/*** Execute the given {@code task}.* <p>The call might return immediately if the implementation uses* an asynchronous execution strategy, or might block in the case* of synchronous execution.* @param task the {@code Runnable} to execute (never {@code null})* @throws TaskRejectedException if the given task was not accepted*/@Overridevoid execute(Runnable task);}此接⼝和Executor⼏乎完全⼀样,只定义了⼀个接收Runnable参数的⽅法,据Spring官⽅介绍此接⼝最初是为了在其他组建中使⽤线程时,将JKD抽离出来⽽设计的。
在Spring的⼀些其他组件中⽐如ApplicationEventMulticaster,Quartz都是使⽤TaskExecutor来作为线程池的抽象的。
spring中的定时任务<!-- 定期评价线程 start --><bean name="autoEvaluateSQM" singleton="false"class="com.norteksoft.supply.evaluate.web.AutoEvaluateSQM"><property name="quartzTimeConfigManager"><ref bean="quartzTimeConfigManager"/></property><property name="monthEvaluateManager"><ref bean="monthEvaluateManager"/></property></bean><!-- 定义调用对象和调用对象的方法 --><bean id="jobtask_evaluate"class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <!-- 调用的类 --><property name="targetObject"><ref bean="autoEvaluateSQM"/></property><!-- 调用类中的方法 --><property name="targetMethod"><value>autoEvaluation</value></property></bean><!-- 定义触发时间 --><bean id="doTime_evaluate"class="org.springframework.scheduling.quartz.CronTriggerBean"><property name="jobDetail"><ref bean="jobtask_evaluate"/></property><!-- cron表达式 --><property name="cronExpression"><value>0 0/3 * * * ? </value></property></bean><!-- 总管理类如果将lazy-init='false'那么容器启动就会执行调度程序 --><bean id="startQuertz" autowire="no"class="org.springframework.scheduling.quartz.SchedulerFactoryBean"><property name="triggers"><list><ref bean="doTime_evaluate"/></list></property></bean>我们在做项目的过程当中通常会遇到定时任务的概念:(定义某一个时刻让它去执行一个功能--自动备份数据库等等)。
下面我们就来看看spring中的定时任务是如何来工作的。
首先先看它的一个配置文件:application-scheduler.xml.<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN""/dtd/spring-beans.dtd" ><beans><bean id="quartzService" class="s.service.QuartzService"></bean><bean id="reportJbo"class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> <property name="targetObject"><ref bean="quartzService" /></property><property name="targetMethod"><value>start</value> ------action当中要执行的方法------</property><property name="concurrent" value="false" /></bean><bean id="cronReportTrigger"class="org.springframework.scheduling.quartz.CronTriggerBean"><property name="jobDetail"><ref bean="reportJbo" /></property><property name="cronExpression"><value>0 0/1 * * * ?</value> ----每隔多长的时间来执行一个任务---</property></bean><bean id="start"class="org.springframework.scheduling.quartz.SchedulerFactoryBean"><property name="triggers"><list><ref bean="cronReportTrigger" /></list></property></bean></beans>我在QuartzService类中有一个方法为start()public String start() {System.out.println("执行了");}说明:这个 start方法就是spring要定时执行的方法了。
大家可以根据自己的业务来实行相应的内容。
web.xml当中配置我的application-scheduler.xml是放在WEB-INF下面。
<context-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/application-scheduler.xml</param-value></context-param>大家用的时候可以直接把代码复制过去名字一改就可以用了!希望对大家有所帮助。
(2):顺便看看timer的用法了。
如果要在程序中定时执行任务,可以使用java.util.Timer这个类实现。
使用Timer类需要一个继承了java.util.TimerTask的类。
TimerTask是一个虚类,需要实现它的run方法,实际上是他implements 了Runnable接口,而把run方法留给子类实现;下面看一个例子:package com.sy.game.test;import java.util.Timer;import java.util.TimerTask;public class TimeTask {public static void main(String[] args) {TimeTask tTask=new TimeTask();tTask.timeVoid();}public void timeVoid(){final Timer timer = new Timer();TimerTask tt=new TimerTask() {@Overridepublic void run() {System.out.println("到点啦!");timer.cancel();}};timer.schedule(tt, 3000);}}发表时间:2009-06-05 我按照你的方法,Tomcat启动的时候一直报:ng.NoSuchMethodError:mons.collections.SetUtils.orderedSet(Ljava/util/Set;)Ljava/util/Set; 错误,需要的.jar文件都加上了呀spring执行定时任务TimeTask方式<bean id="autoSpiderBackGroundThread" class="AutoSpiderBackGroundThread"/><bean id="scheduledTask"class="org.springframework.scheduling.timer.ScheduledTimerTask"><property name="timerTask"><ref bean="autoSpiderBackGroundThread"/></property><!-- 任务执行周期 2m 关于一些任务的参数请参考JDK doc文档和Spring相关文档--><property name="period"><value>60000</value></property><!-- 延时1m 执行任务 --><property name="delay"><value>1000</value></property></bean><!-- 启动定时器 --><bean id="timerBean" class="org.springframework.scheduling.timer.TimerFactoryBean"> <property name="scheduledTimerTasks"><list><ref bean="scheduledTask"/></list></property></bean>import java.util.TimerTask;import org.apache.log4j.Logger;public class AutoSpiderBackGroundThread extends TimerTask{protected Logger log = Logger.getLogger(AutoSpiderBackGroundThread.class);public static int i = 0;public AutoSpiderBackGroundThread(){}@Overridepublic void run(){log.debug("第"+i+"次请求...");i++;}}Quartz方式<!-- 配置 --><bean name="randomPriceJob"class="org.springframework.scheduling.quartz.JobDetailBean"><property name="jobClass"><value>AutoSpiderBackGroundThread</value></property><property name="jobDataAsMap"><map><entry key="timeout"><value>5</value></entry></map></property></bean><!-- 配置触发器 --><bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean"><property name="jobDetail"><ref bean="randomPriceJob"/></property><!-- 每天的11点到11点59分中,每分钟触发RandomPriceJob,具体说明见附录 --><property name="cronExpression"><value>0 * 11 * * ?</value></property></bean><bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"><!-- 添加触发器 --><property name="triggers"><list><ref local="cronTrigger"/></list></property></bean>import org.apache.log4j.Logger;import org.quartz.JobExecutionContext;import org.quartz.JobExecutionException;import org.springframework.scheduling.quartz.QuartzJobBean;public class AutoSpiderBackGroundThread extends QuartzJobBean{protected Logger log = Logger.getLogger(AutoSpiderBackGroundThread.class);public static int i = 0;private int timeout;public AutoSpiderBackGroundThread(){}public void setTimeout( int timeout ){this.timeout = timeout;}@Overrideprotected void executeInternal(JobExecutionContext arg0) throws JobExecutionException { log.debug("自动采集进程启动...");}}import org.apache.log4j.Logger;import org.quartz.JobExecutionContext;import org.quartz.JobExecutionException;import org.springframework.scheduling.quartz.QuartzJobBean;public class AutoSpiderBackGroundThread extends QuartzJobBean{protected Logger log = Logger.getLogger(AutoSpiderBackGroundThread.class);public static int i = 0;private int timeout;public AutoSpiderBackGroundThread(){}public void setTimeout( int timeout ){this.timeout = timeout;}@Overrideprotected void executeInternal(JobExecutionContext arg0) throws JobExecutionException { log.debug("自动采集进程启动...");}}使用Spring Quartz执行定时任务spring 2008-12-15 16:48 阅读21 评论0 字号:大大中中小小 QuartzTest.javaimport org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class QuartzTest{public void sayHello() {System.out.println("HelloWorld! ");}public static void main(String[] args )throws Exception{ApplicationContext context = newClassPathXmlApplicationContext("applicationContext.xml");}}**********************************************************************applicationContext.xml<?xml version="1.0" encoding="UTF-8"?><beans xmlns="/schema/beans"xmlns:xsi="/2001/XMLSchema-instance"xsi:schemaLocation="/schema/beans/schema/beans/spring-beans-2.0.xsd"><bean id="quartztest" class="QuartzTest" /><bean id="quartztest_JobDetail"class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"><property name="targetObject"><ref bean="quartztest" /></property><property name="targetMethod"><value>sayHello</value>。