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;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值