Project use:spring+springmvc+jpa+maven
1. referencing in Pom.xml Quartz of the Jar Package
<!--quartz--><dependency> <groupId>org.quartz-scheduler</groupId> <artifactId> Quartz</artifactid> <version>2.2.1</version></dependency>
Quartz need Spring-context-support-3.2.4.release.jar etc (quartz2.x version required spring3.1 above version support, specific please Baidu)
2. Task class (does not inherit the job interface)
Package Com.test.utils.quartz;import Java.io.ioexception;import Java.io.inputstream;import java.util.Properties; Import javax.annotation.resource;import com.test.service.order.iorderservice;/** * Quartz Timer-Order * * @author cc * */publi C class Quartzjoboforder {//Service@resource (name = "Testservice") private itestservice testservice;public void work () {/ /do something. You can use the service, DAO Layer Method}}
3. Add Quartz configuration to spring configuration file
<!-- Work class to invoke --><bean id= "Quartzjob_1" class= "Com.test.utils.quartz.QuartzJobOfOrder" ></bean><!-- defining methods for calling objects and calling objects --> <bean id= "Jobtask_1" class= " Org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean "><!-- called classes -->< Property name= "TargetObject" ><ref bean= "quartzjob_1" /></property><!-- Calling a method in a class --><property name= "Targetmethod" ><value>work</value></property> </bean><!-- Define trigger time --><bean id= "crontrigger_1" class= " Org.springframework.scheduling.quartz.CronTriggerFactoryBean "><property name=" Jobdetail "><ref bean= "Jobtask_1" /></property><!-- 0 0 23 L * ? The last day of the month between 23 points ;0/10 * * * * ? every 10 seconds to execute --><property name= "CroNexpression "><value>0 0 23 L * ?</value></property></bean> <!-- General management If you lazy-init= ' false ' then the container starts executing the scheduler --><bean id= "Startquertz" Lazy-init= "false" autowire= "no" class= "Org.springframework.scheduling.quartz.SchedulerFactoryBean" >< Property name= "triggers" ><list><ref bean= "crontrigger_1" /></list></ Property></bean>
With the above configuration, you can use the quartz timer.
There is no way to use annotations, there is time to get it.
Spring+quartz Timer use