quartz的job怎么获取Spring上下文
第一步、在org.springframework.scheduling.quartz.SchedulerFactoryBean对象中注入applicationContextSchedulerContextKey
<bean id="startQuertz" lazy-init="false" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="applicationContextSchedulerContextKey" value="applicationContext" />
</bean>
注入applicationContextSchedulerContextKey对象后,SchedulerFactoryBean会将Spring的applicationContext加入到quartz的SchedulerContext中(见下图源码)。

第二步:通过SchedulerContext获取ApplicationContext,下面代码中的applicationContext就是上面applicationContextSchedulerContextKey配置的值。
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.SchedulerContext;
import org.quartz.SchedulerException;
import org.springframework.context.ApplicationContext; public class TestJob implements Job { @Override
public void execute(JobExecutionContext context) throws JobExecutionException {
try {
SchedulerContext schedulerContext = context.getScheduler().getContext();
ApplicationContext applicationContext = (ApplicationContext) schedulerContext.get("applicationContext"); System.out.println(applicationContext.getId());
} catch (SchedulerException e) {
e.printStackTrace();
}
}
}
quartz的job怎么获取Spring上下文的更多相关文章
- JAVA获取Spring上下文
1. 添加监听 public class SpringContextListener implements ServletContextListener { //获取spring注入的bean对象 p ...
- 五)Spring + Quartz 复杂业务的两个问题:获取Spring上下文 和 自动注入服务类
配置如下: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http:// ...
- 基于Spring DM管理的Bundle获取Spring上下文对象及指定Bean对象
在讲述服务注册与引用的随笔中,有提到context.getServiceReferences()方法,通过该方法可以获取到OSGI框架容器中的指定类型的服务引用,从而获取到对应的服务对象.同时该方法还 ...
- 获取spring上下文 - applicationContext
前言 spring上下文是spring容器抽象的一种实现.将你需spring帮你管理的对象放入容器的一种对象,ApplicationContext是一维护Bean定义以及对象之间协作关第的高级接口. ...
- 获取spring上下文的bean 工具类
有些场景我们不属于controller,service,dao,但是我们需要从spring中得到spring容器里面的bean.这时候我们需要一个类继承 ApplicationContextAware ...
- Spring +quartz获取ApplicationContext上下文
job存在数据库中,能够进行动态的增增删改查,近期遇到了怎样获取ApplicationContext上下文的问题.解决的方法例如以下 applicationContext-quartz.xml < ...
- 【Spring】非Spring IOC容器下获取Spring IOC上下文的环境
前言 在Spring Web项目中,有些特殊的时候需要在非Spring IOC容器下获取Spring IOC容器的上下文环境,比如获取某个bean. 版本说明 声明POM文件,指定需引入的JAR. & ...
- 获取Spring的上下文环境ApplicationContext的方式
摘自: http://blog.csdn.net/yang123111/article/details/32099329 获取Spring的上下文环境ApplicationContext的方式 Web ...
- 一种获取spring环境上下文方法:SpringContextUtil
获得spring里注册Bean的有好几种方法,这里介绍一种比较简单的方法: import org.springframework.beans.BeansException; import org.sp ...
随机推荐
- spring--给配置文件.properties加密
11111111111编写类并继承PropertyPlaceholderConfigurer.java package com.xx.encryptDecrypt; import java.util. ...
- ArchLinux安装Sublime Text 3
安装方法: 在 /etc/pacman.conf中添加 [archlinuxcn] SigLevel = Optional TrustAll Server = http://repo.archlinu ...
- spring cloud zuul参数调优
zuul 内置参数 zuul.host.maxTotalConnections 适用于ApacheHttpClient,如果是okhttp无效.每个服务的http客户端连接池最大连接,默认是200. ...
- xshell 利用密钥登录
第一步:新建用户密钥 第二步:选择加密方式,密钥长度越长越安全 第三步:设置密钥名称和密码(密码可为0,这里是密钥的密码非服务器密码) 第四步:保存公钥到本地 第五步:导出私钥到本地 第六步:将公钥和 ...
- PM九步法
本文转载自网络. 多年以后,当我面对那些年青的产品经理,我会想起自己当年从事的是一份高薪的工作.那是2000年,我大学毕业后在北京一家IT网站做搜索引擎PM,当时我一个月的薪水能在亚运村买一平方米房子 ...
- Spring整合MyBatis(简单登录Demo)
SpringMvc简单整合(登录模块) 1.1 整合思路 1.SqlSessionFactory对象应该放到spring容器中作为单例存在. 2.传统dao的开发方式中,应该从spring容器中获得s ...
- 解决CentOS6.5虚拟机克隆后无法上网(网卡信息不一致)的问题
一.问题描述 虚拟机克隆后,由于网卡信息不一致的问题,导致不能上网或者执行“service network restart”命令失败 [root@lyy 桌面]# ifconfig //查看当前网卡信 ...
- 连接redis错误:ERR Client sent AUTH, but no password is set
问题原因:没有设置redis的密码 解决:命令行进入Redis的文件夹: D:\Redis-x64-3.2.100>redis-cli.exe 查看是否设置了密码: 127.0.0.1:6379 ...
- Injection的简单辨析
依赖注入(injection)是一种对任何编程语言都有效的概念.依赖注入背后的一般概念称为控制反转.根据这个概念,类不应该静态配置其依赖项,而应该从外部配置. 如果Java类使用此类的实例,则Java ...
- eclipse Oxygen2 4.7.2版本安装activiti插件,并兼容svn插件
附录,插件下载:链接:https://pan.baidu.com/s/138ChoXao1fALBzdOhJjdQg 密码:06fx 安装方法: 解压eclipse安装包,将eclipse-activ ...