problem
When we use the Quartz, Job dependency injection in Spring Bean dependency injection occurs when an object is null, this time need to how to solve this problem?
answer
Only need to add the execute method SpringBeanAutowiringSupport.processInjectBasedOnCurrentContext (this) can solve this problem. code show as below
public class TaskJob implements Job {
private final Logger logger = LoggerFactory.getLogger(TaskJob.class);
@Autowired
private TaskService taskService;
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
//重点是加入这句代码,那么就可以向该类中依赖注入spring bean了
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
try {
taskService.scanVsphereById(jobExecutionContext.getJobDetail().getKey().getName());
} catch (Exception e) {
e.printStackTrace();
}
logger.info(jobExecutionContext.getJobDetail().getKey().getName() +
"成功执行定时任务-------------------------------------------------");
}
}