shaneoeo 2018-12-27 15:02 采纳率: 0%
浏览 1430

如何在Quartz的Job类中获取到Service?

请问如何在Quartz的Job类中获取到Service,我在Job类中获取Service都是Null。
使用网上的方法也没有效果,求助!

报错信息

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)

声明定时任务

       long time = System.currentTimeMillis()+2000;
            SchedulerFactory sf = new StdSchedulerFactory();
            Scheduler sched = sf.getScheduler();
            Date runTime = DateBuilder.evenSecondDate(testTimestampToDate(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类

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

获取Bean工具类

@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>
  • 写回答

4条回答 默认 最新

  • L星火燎原 2018-12-27 07:17
    关注

    RefundService有没有在XML中声明Bean??

    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)