bbzhshw
alexhometown
采纳率0%
2019-05-23 09:11 阅读 1.5k

Spring bean中的自动注入无法注入,显示为null?

涉及到的两个类的代码如下:

@Configuration
public class QuartzConfig extends AbstractApplicationContextAware {

    @Bean(name="stdScheduler")
    public Scheduler stdSchedulerFactory() throws SchedulerException{
        StdSchedulerFactory factory = new StdSchedulerFactory();
        Scheduler scheduler = factory.getDefaultScheduler();
        return scheduler;
    }

    @DependsOn(value={"stdScheduler","qrtzJobDao","qrtzTriggerDao"})
    @Bean(name="schedulerDispatcher")
    public SchedulerDispacher schedulerDispacher(){
        return new SchedulerDispacher();
    }

}

public class SchedulerDispacher extends AbstractApplicationContextAware implements InitializingBean {

    private ApplicationContext appContext;
    private HashMap<String,QrtzTriggerBean> triggersMap = new HashMap<String, QrtzTriggerBean>();
    private HashMap<String,QrtzJobBean> jobsMap = new HashMap<String, QrtzJobBean>();

    //保存已注册的trigger和job
    private final HashMap<String, Trigger>   registedTriggers = new HashMap<String, Trigger>();
    private final HashMap<String, JobDetail> registedJobs     = new HashMap<String, JobDetail>();

    @Autowired
    private Scheduler scheduler;
    @Autowired
    private QrtzJobDao qrtzJobDao;
    @Autowired
    private QrtzTriggerDao qrtzTriggerDao;



    /**
     * 所需属性初始化后开始初始化schedule
     * 
     * 为什么不找job?,因为一个Job class可以绑定给多个Trigger,
     * 运行中的job 实例与 Trigger才是一对一的关系
     */
    @Override
    public void afterPropertiesSet() {

        appContext = getApplicationContext();
        //qrtzJobDao
        //qrtzTriggerDao
        System.out.println("####^^^^"+scheduler==null);
        System.out.println("####^^^^"+qrtzJobDao==null);
        System.out.println("####^^^^"+qrtzTriggerDao==null);

        try {
            findTriggerEligible();          //找到需要运行的trigger

            scheduleJobAndTrigger();        //将job与trigger绑定到scheduler中去

            scheduler.startDelayed(5);      //等待5秒后开始定时任务

        } catch (SchedulerException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }


    }

    /**
     * 找到合乎条件的Trigger
     * @throws ClassNotFoundException
     * @throws SchedulerException
     */
    private void findTriggerEligible() throws ClassNotFoundException, SchedulerException{
        QueryWrapper<QrtzTriggerBean> queryCondition = new QueryWrapper<QrtzTriggerBean>();
        queryCondition.eq("status", "1");   //条件

        List<QrtzTriggerBean> trigBeans = qrtzTriggerDao.selectList(queryCondition);

        for(QrtzTriggerBean tBean : trigBeans){
            triggersMap.put(tBean.getTriggerName(), tBean);

            //trigger绑定的job
            QrtzJobBean jBean = qrtzJobDao.selectById(tBean.getJobId());
            tBean.setJob(jBean);

            jobsMap.put(jBean.getJobName(), jBean);

        }
    }

    /**
     * 将job和trigger绑定到scheduler上
     * 
     * @author zhou
     * @time   上午10:41:34
     */
    private void scheduleJobAndTrigger() throws ClassNotFoundException, SchedulerException{
        Set<String> keys = triggersMap.keySet();
        for(String key : keys){
            QrtzTriggerBean t = triggersMap.get(key);
            QrtzJobBean     j = jobsMap.get(t.getJob().getJobName());

            JobDetail job     = JobProducer.produceJob(j);
            Trigger   trigger = TriggerProducer.produceTrigger(t);

            registedTriggers.put(t.getTriggerName(), trigger);
            registedJobs.put(j.getJobName(), job);

            scheduler.scheduleJob( job,trigger );
        }
    }

    /**
     * 暂时trigger
     * 
     * @param name
     * @throws SchedulerException
     */
    public void pauseTrigger(String triggerName) throws SchedulerException{
        scheduler.pauseTrigger(findTriggerKeyByName(triggerName));
    }

    /**
     * 恢复暂停的trigger
     * 
     * @author zhou
     * @throws SchedulerException 
     * @time   上午10:43:45
     */
    public void revertPausedTrigger(String triggerName) throws SchedulerException{
        scheduler.resumeTrigger(findTriggerKeyByName(triggerName));
    }

    private TriggerKey findTriggerKeyByName(String triggerName){
        return registedTriggers.get(triggerName).getKey();
    }


}

现在遇到的问题是:
afterPropertiesSet()方法中用到dao都null
报错信息部分如下:

Caused by: java.lang.NullPointerException
    at com.elefirst.schedule.backend.util.SchedulerDispacher.findTriggerEligible(SchedulerDispacher.java:102)
    at com.elefirst.schedule.backend.util.SchedulerDispacher.afterPropertiesSet(SchedulerDispacher.java:78)

请问这个怎么解决?是bean初始化与自动注入先后顺序导致的吗?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • qq_41462304 月存5k的欢乐外包仔 2019-05-23 16:16

    老哥,你这个定时任务玩的太复杂了,我有简单的,看你想不想了解一下

    点赞 评论 复制链接分享

相关推荐