Spring中ApplicationContextAware接口用法
来源:互联网 发布:德拉蒙德 格林体测数据 编辑:程序博客网 时间:2024/05/20 13:09
加载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接口用法
- Spring中ApplicationContextAware接口的用法
- spring中ApplicationContextAware接口
- spring中ApplicationContextAware接口ServletContextAware接口
- Spring中ApplicationContextAware接口使用理解
- Spring中ApplicationContextAware接口的作用
- Spring中ApplicationContextAware接口的使用
- Spring spring中InitializingBean接口和ApplicationContextAware接口
- spring ApplicationContextAware接口介绍
- Spring ApplicationContextAware接口
- Spring中通过实现ApplicationContextAware接口获取bean
- Spring中ApplicationContextAware使用说明
- Spring中ApplicationContextAware的理解
- Spring中ApplicationContextAware的作用
- Spring接口ApplicationContextAware介绍及使用
- spring中多例、BeanFactoryAware、ApplicationContextAware、InitializingBean接口
- 通过ApplicationContextAware接口来保存Spring ApplicationContext
- Spring Bean扩展接口 -- ApplicationContextAware源码分析
- 全局事务与本地事务的区别应用
- opencv的入门1
- mac 安装 Package Control 插件(sublime text 2)
- Aandroid 图片加载库Glide 实战(一),初始,加载进阶到实践
- 矩阵快速幂--学习笔记
- Spring中ApplicationContextAware接口用法
- nessus从2005年不再开源,一种离线更新其插件的方法。
- webview加载页面中包含文件下载
- 用JStack和Top分析Java进程CPU占用率
- Swift视频教程_零基础Swift实战开发从入门到精通
- grep 多选
- Egit使用手册
- Python正则表达式
- ADOX.Catalog简易中文文档