在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 ...
随机推荐
- Java NIO SocketChannel
A Java NIO SocketChannel is a channel that is connected to a TCP network socket. It is Java NIO's eq ...
- 如何查看Isilon的节点的CPU的信息?
使用Isilon自带的命令 isi_hw_status 使用如下的命令 dmidecode --type processor 笔者只是想了解一下F800的CPU有多少个core.上面的命令都可以获得这 ...
- RV LayoutManager 流式布局 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- 【Spark】Sparkstreaming-共享变量-缓存RDD-到底是什么情况?
Sparkstreaming-共享变量-缓存RDD-到底是什么情况? sparkstreaming 多个 rdd_百度搜索 Spark Streaming中空RDD处理及流处理程序优雅的停止 - xu ...
- VIM的buffers
原文:http://ju.outofmemory.cn/entry/13522 重新在不同的 tab 中打开多个关闭的buffer 文件, https://stackoverflow.com/ques ...
- idea缓存目录mac cache
IDEA如果出现卡顿,Index疯狂扫描,建议清空一下如下目录 ~/Library/Caches/IntelliJIdea2017.3 Resource nexus-maven-repository- ...
- Maven hive-jdbc教程
由于项目中需要用到hive-jdbc从数据仓库拉数据下来,所以简单的学一下hive,hive数据仓库建构在hadoop集群之上,数据存在hdfs文件系统中,hive中执行的操作会装换成mapreduc ...
- [R]Kick start
- VC++导出具有命名空间的函数
问题现象 原因分析 解决的方法 1 问题现象 导出具有命名空间的函数和类.源码例如以下: 头文件MiniMFC.h namespace MiniMFC { __declspec(dllexport) ...
- android中Snackbar(Design Support)的使用
Snackbar是Android Design Support Library中的一个组件,想使用Snackbar,必须先引入Design Support,我这里引入的是当前的最新版本: implem ...