Spring Quartz job cannot rely on injection, Spring Integration quartz job task cannot inject

Source: Internet
Author: User

Spring Quartz job cannot rely on injection, Spring Integration quartz job task cannot inject

Spring4 Integration quartz2.2.3 Job Task Usage @Autowired can't inject

>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>

©Copyright Sweet Potato Yiu September 6, 2017

http://www.cnblogs.com/fanshuyao/

First, the problem description:

When using spring to integrate quartz for dynamic tasks, when you want to use a service in a Job scheduled task, you can simply annotate @component, @Autowired cannot be injected, and get the object null. As in the following code:

Java code
    1. @Component
    2. @PersistJobDataAfterExecution
    3. @DisallowConcurrentExecution
    4. Public class Ticketsalepricelessthanlowestpricejob implements job{
    5. @Autowired
    6. private Xxxservice Xxxservice;
    7. }

Second, the solution:

1, add a custom Class (Customjobfactory), inherit Springbeanjobfactory, the code is as follows:

Java code
  1. Import Org.quartz.spi.TriggerFiredBundle;
  2. Import org.springframework.beans.factory.annotation.Autowired;
  3. Import Org.springframework.beans.factory.config.AutowireCapableBeanFactory;
  4. Import Org.springframework.scheduling.quartz.SpringBeanJobFactory;
  5. Public class Customjobfactory extends springbeanjobfactory{
  6. @Autowired
  7. private Autowirecapablebeanfactory capablebeanfactory;
  8. @Override
  9. protected Object createjobinstance (Triggerfiredbundle bundle) throws Exception {
  10. //method of calling parent class
  11. Object jobinstance = super.createjobinstance (bundle);
  12. //To inject
  13. Capablebeanfactory.autowirebean (jobinstance);
  14. return jobinstance;
  15. }
  16. }

2, in the Spring.xml file configuration Customjobfactory, as follows:

Java code
    1. <bean id="customjobfactory" class="Cn.imovie.manage.task.job.CustomJobFactory" ></bean>

3. Inject the custom customjobfactory into the Org.springframework.scheduling.quartz.SchedulerFactoryBean as follows:

Java code
    1. <property name="jobfactory" ref="customjobfactory" ></property>

The complete code is as follows:

Java code
  1. <!--timed task configuration start-to-
  2. <bean id="Scheduler" class="Org.springframework.scheduling.quartz.SchedulerFactoryBean" >
  3. <property name="DataSource" ref="DataSource" ></property>
  4. <!--optional, Quartzscheduler update your existing job on startup, so you don't have to delete the Qrtz_job_details table record after each modification targetobject
  5. <property name="overwriteexistingjobs" value="true"/>
  6. <!--required, Quartzscheduler delay start, Quartzscheduler Restart after application start-up
  7. <property name="Startupdelay" value="ten"/>
  8. <!--settings Auto-start
  9. <property name="Autostartup" value="true"/>
  10. <property name="jobfactory" ref="customjobfactory" ></property>
  11. <property name="Applicationcontextschedulercontextkey" value="Applicationcontextkey"/>
  12. <property name="configlocation" value="classpath:spring-quartz.properties"/>
  13. </bean>
  14. <!--timed task configuration End--

4. You can then inject the service using @autowired in the Job task class.

>>>>>>>>>>>>>>>>>>>>>>>>>>> >>>>>

©Copyright Sweet Potato Yiu September 6, 2017

http://www.cnblogs.com/fanshuyao/

Spring Quartz job cannot rely on injection, Spring Integration quartz job task cannot inject

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.