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
- @Component
- @PersistJobDataAfterExecution
- @DisallowConcurrentExecution
- Public class Ticketsalepricelessthanlowestpricejob implements job{
- @Autowired
- private Xxxservice Xxxservice;
- }
Second, the solution:
1, add a custom Class (Customjobfactory), inherit Springbeanjobfactory, the code is as follows:
Java code
- Import Org.quartz.spi.TriggerFiredBundle;
- Import org.springframework.beans.factory.annotation.Autowired;
- Import Org.springframework.beans.factory.config.AutowireCapableBeanFactory;
- Import Org.springframework.scheduling.quartz.SpringBeanJobFactory;
- Public class Customjobfactory extends springbeanjobfactory{
- @Autowired
- private Autowirecapablebeanfactory capablebeanfactory;
- @Override
- protected Object createjobinstance (Triggerfiredbundle bundle) throws Exception {
- //method of calling parent class
- Object jobinstance = super.createjobinstance (bundle);
- //To inject
- Capablebeanfactory.autowirebean (jobinstance);
- return jobinstance;
- }
- }
2, in the Spring.xml file configuration Customjobfactory, as follows:
Java code
- <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
- <property name="jobfactory" ref="customjobfactory" ></property>
The complete code is as follows:
Java code
- <!--timed task configuration start-to-
- <bean id="Scheduler" class="Org.springframework.scheduling.quartz.SchedulerFactoryBean" >
- <property name="DataSource" ref="DataSource" ></property>
- <!--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
- <property name="overwriteexistingjobs" value="true"/>
- <!--required, Quartzscheduler delay start, Quartzscheduler Restart after application start-up
- <property name="Startupdelay" value="ten"/>
- <!--settings Auto-start
- <property name="Autostartup" value="true"/>
- <property name="jobfactory" ref="customjobfactory" ></property>
- <property name="Applicationcontextschedulercontextkey" value="Applicationcontextkey"/>
- <property name="configlocation" value="classpath:spring-quartz.properties"/>
- </bean>
- <!--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