第一步、在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上下文的更多相关文章

  1. JAVA获取Spring上下文

    1. 添加监听 public class SpringContextListener implements ServletContextListener { //获取spring注入的bean对象 p ...

  2. 五)Spring + Quartz 复杂业务的两个问题:获取Spring上下文 和 自动注入服务类

    配置如下: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http:// ...

  3. 基于Spring DM管理的Bundle获取Spring上下文对象及指定Bean对象

    在讲述服务注册与引用的随笔中,有提到context.getServiceReferences()方法,通过该方法可以获取到OSGI框架容器中的指定类型的服务引用,从而获取到对应的服务对象.同时该方法还 ...

  4. 获取spring上下文 - applicationContext

    前言 spring上下文是spring容器抽象的一种实现.将你需spring帮你管理的对象放入容器的一种对象,ApplicationContext是一维护Bean定义以及对象之间协作关第的高级接口. ...

  5. 获取spring上下文的bean 工具类

    有些场景我们不属于controller,service,dao,但是我们需要从spring中得到spring容器里面的bean.这时候我们需要一个类继承 ApplicationContextAware ...

  6. Spring +quartz获取ApplicationContext上下文

    job存在数据库中,能够进行动态的增增删改查,近期遇到了怎样获取ApplicationContext上下文的问题.解决的方法例如以下 applicationContext-quartz.xml < ...

  7. 【Spring】非Spring IOC容器下获取Spring IOC上下文的环境

    前言 在Spring Web项目中,有些特殊的时候需要在非Spring IOC容器下获取Spring IOC容器的上下文环境,比如获取某个bean. 版本说明 声明POM文件,指定需引入的JAR. & ...

  8. 获取Spring的上下文环境ApplicationContext的方式

    摘自: http://blog.csdn.net/yang123111/article/details/32099329 获取Spring的上下文环境ApplicationContext的方式 Web ...

  9. 一种获取spring环境上下文方法:SpringContextUtil

    获得spring里注册Bean的有好几种方法,这里介绍一种比较简单的方法: import org.springframework.beans.BeansException; import org.sp ...

随机推荐

  1. Hive为什么要启用Metastore?

    转载来自: https://blog.csdn.net/qq_40990732/article/details/80914873 https://blog.csdn.net/tp15868352616 ...

  2. SpringMVC控制器方法参数传入的ModelMap 和Model类型有啥区别

    参考 http://blog.csdn.net/u013067598/article/details/69372309 http://blog.csdn.net/u013686993/article/ ...

  3. JMeter_方案上架,遇到的问题及解决

    问题:手动输入时间接口正确,但是不会在前端展示 解决:发布时间.加入时间设置为服务器时间,服务器时间通过接口自动获取到

  4. 关于 ionic3 tabs 导航ico 点击 页面返回顶部

    类似微信 双击 页面返回顶部功能,ionic3 中有一个 Content. 将 import { Content } from 'ionic-angular'; 放入想要实现此功能的 ts中. 实例化 ...

  5. linux C access

    [1]作用 确定文件的访问权限 [2]头文件 #include <unistd.h> [3]函数定义 int access(const char * pathname,  int mode ...

  6. flask_mail发送163邮件,报553错误的原因

    最近在练习用flask_mail发送163邮件时报错: reply: '553 authentication is required,163 smtp9,DcCowAD3eEQZ561caRiaBA- ...

  7. Shell 文本处理命令

    命令:cut –d’:’ -f1, 文件名 #切割处文件列的参数. -d切割字符. -f列的第几个参数. -c1-10指定字符串范围行的第一个到第十个. 命令:sort 文件名 #根据第一列第一个字符 ...

  8. 设计模式理解(九)结构型——外观(Facade)

    等了好久,终于想起来开写了,这次写的是外观模式,记得大学时弄课程设计,外观模式搞得我比较混乱,因为单词不认识,后来觉得有点蛋疼,感觉是一坨混乱的东西然后加个壳再弄几个外部调用的接口而已.个人认为,Fa ...

  9. msf下的各种生成payload命令

    Often one of the most useful (and to the beginner underrated) abilities of Metasploit is the msfpayl ...

  10. AFNetworking的简单使用

    AFNetworking的下载地址: https://github.com/AFNetworking/AFNetworking AFNetworking的使用非常简单,创建一个类,调用一个方法就可以达 ...