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
Member variables add annotations @autowired
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