One, inherit the Spring Package Quartz Class (Org.springframework.scheduling.quartz.QuartzJobBean) mode
Spring-mvc-quartz2.xml:
<BeanID= "Job2trigger"class= "Org.springframework.scheduling.quartz.CronTriggerBean"> < Propertyname= "Jobdetail"> <Beanclass= "Org.springframework.scheduling.quartz.JobDetailBean"> < Propertyname= "Jobclass"value= "Com.yueguang.schedule.Job2"/> < Propertyname= "Jobdataasmap"> <!-- can only be injected into this property and cannot be injected into the task class. <Map><entryKey= "Job2service"Value-ref= "Job2service"/></Map> </ Property> </Bean> </ Property> <!--Specifying cron Expressions - < Propertyname= "Cronexpression"value= "0/1 * * * *?"/> </Bean><BeanID= "Schedulerfactory"Lazy-init= "false"Autowire= "No"class= "Org.springframework.scheduling.quartz.SchedulerFactoryBean"> < Propertyname= "Triggers"> <List> <refBean= "Job1trigger" /> <!--<ref bean= "Job2trigger"/> - </List> </ Property></Bean>
Second, using the spring Methodinvokingjobdetailfactorybean class, no inheritance is required.
Spring-mvc-quartz1.xml
<!--Job Bean Configuration - <BeanID= "Job1"class= "Org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> < Propertyname= "TargetObject"ref= "Job1bean" /> < Propertyname= "Targetmethod"value= "Justdoit" /> < Propertyname= "Concurrent"value= "true" /> <!--set to True to execute multiple jobs concurrently - </Bean> <!--Trigger - <BeanID= "Job1trigger"class= "Org.springframework.scheduling.quartz.CronTriggerBean"> < Propertyname= "Jobdetail"ref= "Job1" /> < Propertyname= "Cronexpression"value= "0/1 * * * *?" /> </Bean> <!--timed Task Scheduler - <BeanID= "Schedulerfactory"Lazy-init= "false"Autowire= "No"class= "Org.springframework.scheduling.quartz.SchedulerFactoryBean"> < Propertyname= "Triggers"> <List> <refBean= "Job1trigger" /> <!--<ref bean= "Job2trigger"/> - </List> </ Property> </Bean>
Note:
1, in the first way, the Spring bean component cannot be injected directly in Jobbean with annotations, and the bean component to be injected must be configured in the Jobdetail property Jobdataasmap. It is also necessary to write the corresponding setter in the Jobbean.
2, the following project is the MAVEN project.
Complete test Project Download
Two ways to perform scheduled tasks in spring using quartz