Spring中ApplicationContextAware接口用法

 

Spring中ApplicationContextAware接口用法

加载Spring配置文件时,如果Spring配置文件中所定义的Bean类,如果该类实现了ApplicationContextAware接口,那么在加载Spring配置文件时,会自动调用ApplicationContextAware接口中的 public void setApplicationContext(ApplicationContext context) throws BeansException方法,并且自动可获得ApplicationContext 对象。当初始化该类的时候,自动生成上下文对象(ApplicationContext ),Spring配置文件中指定该类,进行初始化。

xml如下:

 

<bean id="schedulerManage" class="com.isoftskill.zc.task.SchedulerManage" depends-on="schedulerDao" init-method="init" />

 

 

程序如下:

package com.isoftskill.zc.task;

import com.isoftskill.zc.constants.SchedulerTypeEnum;
import com.isoftskill.zc.model.dao.SchedulerDao;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import java.util.Date;
import java.util.List;
import java.util.Map;


public class SchedulerManage implements ApplicationContextAware {
    private StdSchedulerFactory stdSchedulerFactory = new StdSchedulerFactory();

    private ApplicationContext applicationContext;

    @Autowired(required = true)//注解,省略get/set方法
    private SchedulerDao schedulerDao;

    public void init() throws Exception {
        Scheduler scheduler = stdSchedulerFactory.getScheduler();
        scheduler.start();

        List<com.isoftskill.zc.model.vo.Scheduler> schedulers_reports =
                schedulerDao.getSchedulerByShType(SchedulerTypeEnum.REPORT_SCHEDULER.getCode());

        List<com.isoftskill.zc.model.vo.Scheduler> schedulers_reportwarnings =
                schedulerDao.getSchedulerByShType(SchedulerTypeEnum.REPORTWARNING_SCHEDULER.getCode());
        for (com.isoftskill.zc.model.vo.Scheduler sh: schedulers_reports) {
             saveJob(sh.getShName(), sh.getShTrigger());
        }
        for (com.isoftskill.zc.model.vo.Scheduler sh: schedulers_reportwarnings) {
             saveJob(sh.getShName(), sh.getShTrigger());
        }
    }

    public void saveJob(String beanName, String triggerReg) throws Exception {
        Scheduler scheduler = stdSchedulerFactory.getScheduler();
        String groupName = "groupName";
        scheduler.pauseJob(beanName, groupName);
        scheduler.deleteJob(beanName, groupName);

        JobDetail job = new JobDetail();
        job.setName(beanName);
        job.setJobClass(ReportSchedulerJob.class);

        Map dataMap = job.getJobDataMap();
        dataMap.put("runMeTask", applicationContext.getBean(beanName));
        job.setGroup(groupName);

        CronTrigger trigger = new CronTrigger();
        trigger.setName("trigger_" + beanName);
        trigger.setCronExpression(triggerReg);
        scheduler.scheduleJob(job, trigger);
    }

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}

你可能感兴趣的:(Spring中ApplicationContextAware接口用法)