Quartz+spring+maven Integration instance (solves the problem of the job implementation class injection Bean is empty)

Source: Internet
Author: User

Environment:

jdk1.7, Eclipse, maven, quartz2.2.1

The schema of the package:

  

Process:

1. Create the project and add the corresponding jar package to the POM

<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd" > <modelversion>4.0.0</    Modelversion> <groupId>com.zluo.quartz</groupId> <artifactId>quartz-demo</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>quartz-demo Maven webapp</name> <url>http://maven.apache.org</url>    <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> &LT;SP Ring.version>3.1.2.release</spring.version> <aspectj.version>1.7.2</aspectj.version> &L T;quartz.version>2.2.1</quartz.version> <slf4j.version>1.7.5</slf4j.version> </propertie S> <dependencies> <!--start Spring--<dependency> &LT;GROUPID&GT;ORG.S Pringframework</groupid> <artifactId>spring-core</artifactId> &LT;VERSION&GT;${SPR ing.version}</version> </dependency> <dependency> <groupid>org.springfram Ework</groupid> <artifactId>spring-aop</artifactId> <version>${spring.versio n}</version> </dependency> <dependency> <groupid>org.springframework</ Groupid> &Lt;artifactid>spring-asm</artifactid> <version>${spring.version}</version> &LT;/DEP endency> <dependency> <groupId>org.springframework</groupId> &LT;ARTIFAC Tid>spring-beans</artifactid> <version>${spring.version}</version> </dependency&        Gt <dependency> <groupId>org.springframework</groupId> <artifactid>spring-conte xt</artifactid> <version>${spring.version}</version> </dependency> <de Pendency> <groupId>org.springframework</groupId> <artifactid>spring-orm</art ifactid> <version>${spring.version}</version> </dependency> &LT;DEPENDENCY&G            T        <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId>    <version>${spring.version}</version> </dependency> <dependency> <g Roupid>org.springframework</groupid> <artifactId>spring-web</artifactId> <ve rsion>${spring.version}</version> </dependency> <dependency> <groupId> Org.springframework</groupid> <artifactId>spring-tx</artifactId> <version>${ spring.version}</version> </dependency> <dependency> &LT;GROUPID&GT;ORG.SPRINGF Ramework</groupid> <artifactId>spring-jms</artifactId> <version>${spring.ver sion}</version> </dependency> <dependency> &LT;GROUPID&GT;ORG.SPRINGFRAMEWORK&L T;/groupid> <artifactId>spring-webmvc</artifactId> &LT;VERSION&GT;${SPRING.VERSION}&L T;/version> &Lt;/dependency> <dependency> <groupId>org.aspectj</groupId> <artifacti D>aspectjweaver</artifactid> <version>${aspectj.version}</version> </dependency&        Gt <!--end Spring--<!--start Quartz--<dependency> <groupid>org.quart Z-scheduler</groupid> <artifactId>quartz</artifactId> <version>${quartz.vers Ion}</version> </dependency> <!--end Quartz--<dependency> < Groupid>c3p0</groupid> <artifactId>c3p0</artifactId> <version>0.9.1.2<            /version> </dependency> <dependency> <groupId>dom4j</groupId>        <artifactId>dom4j</artifactId> <version>1.6.1</version> </dependency> <depEndency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId>            <version>${slf4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <ver sion>${slf4j.version}</version> </dependency> </dependencies> <build> <f inalname>quartz-demo</finalname> <plugins> <plugin> <groupid>o Rg.mortbay.jetty</groupid> <artifactId>maven-jetty-plugin</artifactId> &lt ;version>6.1.10</version> <configuration> <scanintervalseconds>2&lt ;/scanintervalseconds> <connectors> <connector implementation= "org.m Ortbay.jetty.nio.SelectCHannelconnector "> <port>8080</port> &LT;MAXIDLETIME&G                T;60000</maxidletime> </connector> </connectors> </configuration> </plugin> </plugins> </build></project>

2. Add the corresponding configuration file of spring and quartz

#quartz. properies#===============================================================# Configure Main Scheduler properties#===============================================================Org.quartz.scheduler.instanceName=QuartzSchedulerorg.quartz.scheduler.instanceId=auto#===============================================================# Configure threadpool#===============================================================Org.quartz.threadPool.class=Org.quartz.simpl.SimpleThreadPoolorg.quartz.threadPool.threadCount= 5org.quartz.threadPool.threadPriority= 5#===============================================================# Configure jobstore#===============================================================Org.quartz.jobStore.misfireThreshold= 60000Org.quartz.jobStore.class= Org.quartz.simpl.RAMJobStore
<?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"Xmlns:context= "Http://www.springframework.org/schema/context"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"Xmlns:tx= "Http://www.springframework.org/schema/tx"Xmlns:jaxws= "Http://cxf.apache.org/jaxws"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp//Www.springframework.org/schema/contexthttp//www.springframework.org/schema/context/spring-context-2.5.xsdhttp//WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOPhttp//www.springframework.org/schema/aop/spring-aop-2.5.xsdhttp//Www.springframework.org/schema/txhttp//www.springframework.org/schema/tx/spring-tx-2.5.xsdhttp//Cxf.apache.org/jaxwshttp//cxf.apache.org/schemas/jaxws.xsd "><context:annotation-config/> <context:component-scan base- Package= "Com.zluo.quartz"/> <aop:aspectj-autoproxy/> <tx:annotation-driven transaction-manager= " TransactionManager "/> <!--Timer--<bean id=" Jobfactory "class= "Com.zluo.quartz.factory.MJobFactory" ></bean> <bean name= "Schedulejob"class= "Org.springframework.scheduling.quartz.JobDetailFactoryBean" > <property name= "jobclass" value= "Com.zluo.qu          Artz.job.ScheduleJob "/> <property name=" Durability "value=" true "></property> </bean> <bean id= "Schedulejobtriggers"class= "Org.springframework.scheduling.quartz.CronTriggerFactoryBean" > <property name= "Jobdetail" > <ref bean= "Schedulejob"/> </property> <property name= "Cronexpression" > & Lt;value>0 0/1 * * *?</value> </property> </bean> <bean id= "Schedulerfacto Rybean "class= "Org.springframework.scheduling.quartz.SchedulerFactoryBean" destroy-method= "destroy" > <property name= " Jobfactory "ref=" jobfactory "/> <property name=" triggers "> <list> <r EF bean= "schedulejobtriggers"/> </list> </property> <property name= "applic Ationcontextschedulercontextkey "value=" ApplicationContext "/> <property name=" configLocation "value=" class Path:quartz.properties "/> </bean> </beans>

3, the implementation of the development job class

 PackageCom.zluo.quartz.job;ImportJavax.annotation.Resource;ImportOrg.quartz.Job;ImportOrg.quartz.JobExecutionContext;Importorg.quartz.JobExecutionException;ImportCom.zluo.quartz.service.JobService;/*** project Name: Quartz_demo <br> * class Name: Schedulejob <br> * Class Description: <br> * Creator: Louzhangjie <br> * Create Time: May 22, 2015 PM 10:01:04 <br> * Modified by: Louzhangjie <br> * Modified: May 22, 2015 PM 10:01:04 <br> * Revision notes: <BR > *@version1.0 <br> **/ Public classSchedulejobImplementsjob{@ResourcePrivateJobservice Jobservice;  Public voidExecute (Jobexecutioncontext context)throwsjobexecutionexception {jobservice.work (); }}

4. Developing Service entity classes

 Package Com.zluo.quartz.service; Import Org.springframework.stereotype.Service; @Service  Public class Jobservice {    publicvoid work () {        System.out.println ("service work") );    }    }

5. Extend spring's factory to implement Bean injection (details:)

 Packagecom.zluo.quartz.factory;ImportOrg.quartz.spi.TriggerFiredBundle;Importorg.springframework.beans.factory.annotation.Autowired;Importorg.springframework.beans.factory.config.AutowireCapableBeanFactory;Importorg.springframework.scheduling.quartz.AdaptableJobFactory;/*** * Project name: Quartz-demo <br> * class Name: Mjobfactory <br> * Class Description: Used to allow spring to inject in the job's implementation class <br> * Creator: Louz Hangjie <br> * Creation time: May 22, 2015 PM 10:18:12 <br> * Modified by: Louzhangjie <br> * Modified: May 22, 2015 PM 10:18:12 &L t;br> * Revision notes: <br> *@version1.0 <br> **/ Public classMjobfactoryextendsAdaptablejobfactory {//This object spring will help us to automatically inject, also belongs to the Spring Technology category.@AutowiredPrivateautowirecapablebeanfactory capablebeanfactory; protectedObject createjobinstance (Triggerfiredbundle bundle)throwsException {//calling a method of the parent classObject jobinstance =Super. Createjobinstance (bundle); //to inject, this is a spring technology that is not clear to see the Spring API.Capablebeanfactory.autowirebean (jobinstance); returnjobinstance; }}

6. Use command: Jetty:run view test results

End

Good luck

Download code:)

Quartz+spring+maven Integration instance (solves the problem of the job implementation class injection Bean is empty)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.