When you create a scheduled task using the schedule, null pointer resolving method.

When you create a scheduled task using the schedule, null pointer resolving method.

When we use spring boot integration schedule to create a scheduled task, null pointer, probably because when regular tasks like configuration, create job and, since the project received job tasks like creating an object is created through reflection, created by radiation the objects in object does not spring containers, the injection is performed when the traffic class of the task classes can not be injected at the timing. It is necessary to manually spring task class object into a container.
Step:
Create a class 1. Define the task class, and inherit AdaptableJobFactory, this class is responsible for creating the task class objects, but the original class and did not create a good class container into spring, so it is necessary to manually override the method in which the task class container into the spring. When you create a scheduled task using the schedule, null pointer resolving method.
2. The objects created in the previous step into the job in a factory object
When you create a scheduled task using the schedule, null pointer resolving method.

Additional:
Timing class configuration code

package com.example.quartz;

import org.quartz.CronScheduleBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.scheduling.quartz.SimpleTriggerFactoryBean;

@Configuration
public class QuartzConfig {
    //创建job对象
    @Bean
    public JobDetailFactoryBean jobDetailFactoryBean(){
       JobDetailFactoryBean factory =  new JobDetailFactoryBean();
       //factory接收到QuartzJob类对象后,通过反射方式创建对象,并没有放入spring容器中。
       factory.setJobClass(QuartzJob.class);
       return factory;
    }
    //创建simple trigger对象
    @Bean
    public SimpleTriggerFactoryBean simpleTriggerFactoryBean(){
        SimpleTriggerFactoryBean factory = new SimpleTriggerFactoryBean();
        //关联jobDetail对象
        factory.setJobDetail(jobDetailFactoryBean().getObject());
        //间隔毫秒数
        factory.setRepeatInterval(2000);
        //重复次数
        return factory;
    }
    //创建crontrigger
    @Bean
    public CronTriggerFactoryBean  cronTriggerFactoryBean(){
        CronTriggerFactoryBean factory = new CronTriggerFactoryBean();
        factory.setJobDetail(jobDetailFactoryBean().getObject());
        factory.setCronExpression("0/1 * * * * ?");
        return factory;
    }
    @Bean
    public SchedulerFactoryBean schedulerFactoryBean(MyJobDetailFactoryBean myJobDetailFactoryBean){
        SchedulerFactoryBean factory = new SchedulerFactoryBean();
        //关联trigger
        factory.setTriggers(simpleTriggerFactoryBean().getObject());
        //将前面的覆盖掉。
        factory.setTriggers(cronTriggerFactoryBean().getObject());
        factory.setJobFactory(myJobDetailFactoryBean);
        return factory;
    }
}

Guess you like

Origin blog.51cto.com/14423206/2417759
Recommended