How to get Service? in the Job class of Quartz

how to get Service, in the Job class of Quartz and Service is Null in the Job class.
found several methods on the Internet but did not solve it. The code is below. Please take a look at it.

error message

java.lang.NullPointerException
    at com.wufu.util.SpringContextUtil.getBean(SpringContextUtil.java:26)
    at com.xinfu.quartz.QuartzJob.execute(QuartzJob.java:27)
    at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
    at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)

declare timing tasks

   long time = System.currentTimeMillis()+2000;
            SchedulerFactory sf = new StdSchedulerFactory();
            Scheduler sched = sf.getScheduler();
            Date runTime = DateBuilder.evenSecondDate(timestampToDate(time));
            JobDetail job = JobBuilder.newJob(QuartzJob.class)
                    .withIdentity("job1", "group1")
                    .usingJobData("param", 389)
                    .build();
            Trigger trigger = TriggerBuilder.newTrigger()
                    .withIdentity("trigger1", "group1")
                    .startAt(runTime)
                    .build();

Job class

public class QuartzJob implements Job {
    @Override
    public void execute(JobExecutionContext content) throws JobExecutionException {
        try {
            RefundService rs = (RefundService)SpringContextUtil.getBean("RefundService");
            System.out.println(rs.refund());
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

get the Bean utility class

@Component
public class SpringContextUtil implements ApplicationContextAware {
    private static ApplicationContext context;

    @Override
    @SuppressWarnings("static-access" )
    public void setApplicationContext(ApplicationContext contex) throws BeansException {
        this.context = contex;
    }
    public static Object getBean(String beanName){
        return context.getBean(beanName);
    }
    public static String getMessage(String key){
        return context.getMessage(key, null, Locale.getDefault());
    }
    public static Object getBeanByClass(Class elementType) {
        return context.getBean(elementType);
    }
}

ApplicationContext.xml

 <bean id="SpringContextUtil" class="com.XXX.util.SpringContextUtil"  lazy-init="false"></bean>
<bean id="RefundService" class="com.XXX.serv.RefundService"  ></bean>
<bean id="QuartzJob" class="com.XXX.quartz.QuartzJob"></bean>
Mar.23,2022

when declaring Scheduler, put it in JobDataMap and obtain it through JobExecutionContext in Job.


the subject is obtained without a problem. If you don't get it, it's not the right time to get it, that is, when you get RefundService , the bean of RefundService has not been loaded into Spring's IOC. You can set the execution time of Job to verify whether this is the reason. If yes, you can solve the problem by adjusting the loading order of Quartz and bean


@Component
public class NewJob implements Job {
    @Resource
    private HelloService helloService;
}

I have no problem doing this.

Menu