在quartz的Job中获得Spring的WebApplicationContext或ServletContext
有时候我们需要在web工程中定时器类里面获得spring的IOC容器,即WebApplicationContext,用它来获取实现了某接口的所有的bean,因为@Autowired貌似只能注入单个bean。
一开始我是写的一个ServletContextListener,启动服务器的时候就构造定时器并启动,把WebApplicationContext传给定时器的Job,在ServletContextListener中这样得到WebApplicationContext:
- WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
然后在Job中调用webApplicationContext.getBeansOfType(InfoService.class) 得到实现接口的所有bean。
其实,可以更简单,废话少说,这是一个POJO的Job:
- package com.gxjy.job;
- import java.util.Map;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.context.ContextLoader;
- import org.springframework.web.context.WebApplicationContext;
- import com.gxjy.dao.InfoDao;
- import com.gxjy.service.InfoService;
- import com.gxjy.service.runnable.DudeRunner;
- public class ScrawlerJob{
- @Autowired
- private InfoDao infoDao;
- public void execute() {
- WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();
- Map<String, InfoService> map = wac.getBeansOfType(InfoService.class);
- for (InfoService infoService : map.values()) {
- System.out.println("启动:"+infoService.getClass().getName());
- new Thread(new DudeRunner(infoService, infoDao)).start();
- }
- }
- }
重点在
- ContextLoader.getCurrentWebApplicationContext();
这个可以直接获取WebApplicationContext,当然还可以进一步调用getServletContext()就获取到ServletContext了。
这是spring中关于quartz的配置:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
- <bean id="job" class="com.gxjy.job.ScrawlerJob"></bean>
- <bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
- <property name="targetObject">
- <ref bean="job"/>
- </property>
- <property name="targetMethod">
- <value>execute</value>
- </property>
- </bean>
- <bean id="trigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
- <property name="jobDetail">
- <ref bean="jobDetail"/>
- </property>
- <property name="cronExpression">
- <value>0 0 3 * * ?</value>
- </property>
- </bean>
- <bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
- <property name="triggers">
- <list>
- <ref bean="trigger"/>
- </list>
- </property>
- <property name="autoStartup" value="true"></property>
- </bean>
- </beans>
maven依赖除了基本的spring和quartz之外还需要加入spring-context-support的依赖(包含对quartz的支持):
- <pre name="code" class="html"> <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context-support</artifactId>
- <version>4.2.2.RELEASE</version>
- </dependency>
在quartz的Job中获得Spring的WebApplicationContext或ServletContext的更多相关文章
- quartz的job中注入spring对象!
一般情况下,quartz的job中使用autowired注解注入的对象为空,这时候我们就要使用spring-quartz提供的AdaptableJobFactory类. 自定义一个类: public ...
- Spring 梳理-webApplicationContext 与servletContext
1.WebApplicationContext的研究 ApplicationContext是spring的核心,Context通常解释为上下文环境,用“容器”来表述更容易理解一些,Applicatio ...
- 从servlet中获取spring的WebApplicationContext
需要做一个参数初始化类,当web应用被加载时从数据库里取出相关的参数设置 ,并把这些参数放置到application里,jsp页面可以从中取出. 1.在web.xml中配置: <servlet& ...
- 在Servlet中获取spring容器WebApplicationContext
WebApplicationContext springContext = WebApplicationContextUtils.getRequiredWebApplicationContext(ge ...
- Spring+quartz集群配置,Spring定时任务集群,quartz定时任务集群
Spring+quartz集群配置,Spring定时任务集群,quartz定时任务集群 >>>>>>>>>>>>>> ...
- 信步漫谈之Quartz—分布式调度(整合spring早期版本【低于spring3.1】)
一.环境 使用的jar包:spring2.5.6.quartz1.8.6 二.注意点 因为spring内置的quartz版本变化,所以存在spring和quartz版本接口兼容情况,如下: 1)spr ...
- quartz的job怎么获取Spring上下文
第一步.在org.springframework.scheduling.quartz.SchedulerFactoryBean对象中注入applicationContextSchedulerConte ...
- Spring Framework------>version4.3.5.RELAESE----->Reference Documentation学习心得----->Spring Framework中的spring web MVC模块
spring framework中的spring web MVC模块 1.概述 spring web mvc是spring框架中的一个模块 spring web mvc实现了web的MVC架构模式,可 ...
- step6----->往工程中添加spring boot项目------->修改pom.xml使得我的project是基于spring boot的,而非直接基于spring framework
文章内容概述: spring项目组其实有多个projects,如spring IO platform用于管理external dependencies的版本,通过定义BOM(bill of mater ...
随机推荐
- 检索(retrieval && search )-单目标-多目标跟踪-MTMC Tracking和 ReID
跨摄像头多目标跟踪(Multi-Target Multi-Camera Tracking, MTMC Tracking) 跨摄像头多目标跟踪(Multi-Target Multi-Camera Tra ...
- Unique Paths leetcode java
题目: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...
- MFC中位图的显示
分析: 首先,我们要明确一点,窗口的绘制包括两个步骤,首先:擦除窗口背景,然后再对窗口重新进行绘制:当擦除窗口背景时,程序会发生一个WM_ERASEBKGND消息,因此可以在此响应函数中完成位图的显示 ...
- 大数据开发实战:Storm流计算开发
Storm是一个分布式.高容错.高可靠性的实时计算系统,它对于实时计算的意义相当于Hadoop对于批处理的意义.Hadoop提供了Map和Reduce原语.同样,Storm也对数据的实时处理提供了简单 ...
- MongoDB 通过配置文件启动
mongod启动命令需指定一些参数启动服务,我们通过配置文件的方式配置这些参数加载配置. ./bin/mongod --config ./conf/mongodb.conf(或./bin/mongod ...
- TCP/UDP常见端口
著名端口 端口号码 / 层 名称 注释 1 tcpmux TCP 端口服务多路复用 5 rje 远程作业入口 7 echo Echo 服务 9 discard 用于连接测试的空服务 11 systat ...
- retrofit+RXjava二次封装
接入说明:项目中已集成RXjava,RXandroid.Retrofit,为避免包冲突,不须要再次接入. 就可以直接使用RXjava,Retrofit的所有api. github地址:https:// ...
- python 微信企业号
python 微信企业号 准备,如果没有微信企业号,可以先申请体验号记下CorpID和Secret(获取Token用) 发送消息首先可以在微信的开发者中心,查看接口文档 下面就是python代码:1. ...
- activemq 控制面板里的 Number Of Pending Messages、 Messages Enqueued、Messages Dequeued含
Number Of Consumers 消费者 这个是消费者端的消费者数量 Number Of Pending Messages 等待消费的消息 这个是当前未出队列的数量.可以理解为总接收数-总出队 ...
- CAD VC++安装失败 1603错误
问题描述 想安装一个高版本的CAD来着,可安装报错始终报错1603.具体表现为 DESKTOP-F7K8C37 Installing Microsoft Visual C++ 2008 SP1 ...