(v) Two problems with spring + Quartz complex business: Getting the spring context and the auto-injection service class

Source: Internet
Author: User

The configuration is as follows:

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:task= "Http://www.springframework.org/schema/task"Xmlns:context= "Http://www.springframework.org/schema/context"xsi:schemalocation= "Http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd/HTTP Www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd " >    <Context:component-scanBase-package= "Cn.zno" />    <Task:executorID= "Threadpooltaskexecutor"pool-size= "1" />        <Beanclass= "Org.springframework.scheduling.quartz.SchedulerFactoryBean">        < Propertyname= "Jobfactory">            <Beanclass= "Org.springframework.scheduling.quartz.SpringBeanJobFactory" />        </ Property>        < Propertyname= "Applicationcontextschedulercontextkey"value= "Applicationcontextkey" />        < Propertyname= "Startupdelay"value= "0" />        < Propertyname= "Overwriteexistingjobs"value= "true" />        < Propertyname= "Exposeschedulerinrepository"value= "true" />        < Propertyname= "Taskexecutor"ref= "Threadpooltaskexecutor" />        < Propertyname= "Triggers">            <List>                <refBean= "Crontrigger_1" />            </List>        </ Property>    </Bean>    <BeanID= "Crontrigger_1"class= "Org.springframework.scheduling.quartz.CronTriggerFactoryBean">        < Propertyname= "Jobdetail"ref= "Jobdetail_1" />        < Propertyname= "Cronexpression"value="* * * * * ?" />    </Bean>    <BeanID= "Jobdetail_1"class= "Org.springframework.scheduling.quartz.JobDetailFactoryBean">        < Propertyname= "Jobclass"value= "Cn.zno.job.Breathe" />    </Bean>    <BeanID= "Breath"class= "Cn.zno.job.Breathe" /></Beans>

Why do you want to configure this

<name= "Applicationcontextschedulercontextkey"  value= " Applicationcontextkey "/>

Because this key can be used to fetch the spring context.

Problem with configuration 1: cannot be injected automatically.

 PackageCn.zno.job;ImportOrg.quartz.Job;ImportOrg.quartz.JobExecutionContext;Importorg.quartz.JobExecutionException;ImportOrg.springframework.context.ApplicationContext;ImportCn.zno.service.AService; Public classBreatheImplementsJob {//@Autowired//private Aservice Aservice;@Override Public voidExecute (jobexecutioncontext context)throwsjobexecutionexception {Try {//aservice.show ();ApplicationContext CTX = (applicationcontext) Context.getscheduler (). GetContext (). Get ("Applicationcontextkey"); Aservice Aservice= Ctx.getbean (aservice.class);        Aservice.show (); } Catch(Exception e) {e.printstacktrace (); }                    }}

Configure a change: Solve auto-injection problem

Change

        <name= "jobfactory">            <class = "Org.springframework.scheduling.quartz.SpringBeanJobFactory" />        </  Property >

To

        <name= "jobfactory">            <class = "Cn.zno.common.SpringBeanJobFactory" />        </  Property >

Cn.zno.common.SpringBeanJobFactory

 PackageCn.zno.common;ImportOrg.quartz.spi.TriggerFiredBundle;Importorg.springframework.beans.BeansException;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.ApplicationContextAware; Public classSpringbeanjobfactoryextendsorg.springframework.scheduling.quartz.SpringBeanJobFactoryImplementsApplicationcontextaware {PrivateApplicationContext ApplicationContext; @Override Public voidSetapplicationcontext (ApplicationContext applicationcontext)throwsbeansexception { This. ApplicationContext =ApplicationContext; } @OverrideprotectedObject createjobinstance (Triggerfiredbundle bundle)throwsException {Object jobinstance=Super. Createjobinstance (bundle);        Applicationcontext.getautowirecapablebeanfactory (). Autowirebean (jobinstance); returnjobinstance; }}

Another way to achieve this is:

 PackageCn.zno.common;ImportOrg.quartz.spi.TriggerFiredBundle;Importorg.springframework.beans.factory.annotation.Autowired;Importorg.springframework.beans.factory.config.AutowireCapableBeanFactory; Public classSpringbeanjobfactoryextendsorg.springframework.scheduling.quartz.SpringBeanJobFactory {@AutowiredPrivateautowirecapablebeanfactory autowirecapablebeanfactory; @OverrideprotectedObject createjobinstance (Triggerfiredbundle bundle)throwsException {Object jobinstance=Super. Createjobinstance (bundle);        Autowirecapablebeanfactory.autowirebean (jobinstance); returnjobinstance; }}

(v) Two problems with spring + Quartz complex business: Getting the spring context and the auto-injection service class

Related Article

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.