如何在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>

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

改成

SpringContextUtil.getBean("refundServiceImpl");
或者
SpringContextUtil.getBean(RefundService.class);

试试呢!

还不行的话打印一下spring管理的bean信息,看看有没叫 refundService或refundServiceImpl的。

参考代码


    @Autowired
    ApplicationContext applicationContext;


        LOGGER.info("★★★★★★★★ ApplicationContext = {}", applicationContext);
        int i = 1;
        for (String beanName : applicationContext.getBeanDefinitionNames())
        {
            LOGGER.info("{}.\t{}", i, beanName);
            i++;
        } 

你是不是上下文中没有加载到spring配置文件或者是配置信息,导致根本找不到上下文从而获取不到bean,还有就是把job也加入到spring容器中应该可以直接用@Autowire注入其他的bean的吧

首先得把你的Service让Spring容器托管,这样你在使用的时候才能够找到,托管方式太多了,只要是能让Spring识别它是Spring的一个组件就可以,比如在Service实现类上加@Service,或者@Component,或者@Bean,或者直接在xml文件中配置等等,使用的时候可以直接用@Autowired注解注入,这种方法比较方便。