Resolving Spring+quartz cannot automatically inject bean problems

Source: Internet
Author: User

Problem

Sometimes we need to perform some timed tasks (such as batch processing of data), and the more common technical frameworks are in Spring + quartz. There is a problem with this approach: Spring beans cannot be injected automatically.

Environment: Spring3.2.2 + Quartz1.6.1

Quartz configuration:

<BeanID= "Traderriskreportjob"class= "Org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">< Propertyname= "TargetObject"ref= "Tradernoserverresource" />< Propertyname= "Targetmethod"value= "Querytraderno" />< Propertyname= "Concurrent"value= "true" /></Bean>

Service configuration:

<name= "Tradernoserverresource"  class= " Com.test.TraderNoServerResource "><name=" ThreadPool " ref = "ThreadPool" /> </ Bean >  

ThreadPool Configuration:

 <Beanname= "ThreadPool"class= "Org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor" >    < Propertyname= "Corepoolsize"value= "+"></ Property>    < Propertyname= "Maxpoolsize"value= "+"></ Property> </Bean>

The problem that occurs is that threadpool in Tradernoserverresource is null.

Workaround
      1. Member variables add annotations @autowired

      2. Then add the following code in the method (such as the Querytraderno method in the example) to automatically inject the member variable implementation class

        Springbeanautowiringsupport.processinjectionbasedoncurrentcontext (this);

The reasons for this problem are subject to further verification. More than that , quartz is different from the context of SPRINGMVC, and the parent context cannot access the bean in the child context.

Resources

Http://stackoverflow.com/questions/6990767/inject-bean-reference-into-a-quartz-job-in-spring

Resolving Spring+quartz cannot automatically inject bean problems

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.