在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 ...
随机推荐
- 【未解决】centos 6.4 xen4.2 在关机的时候很慢
centos xen 在关机的时候 下面的关闭DomUs虚拟机 耗时很长... stopping xenconsoled daemon: [OK] sending shutdown to all Do ...
- 查看sql执行的情况
今天同事让看一个sql,无意中学到一个查看sql执行情况的命令,以前都是傻傻的等的,今后不用了 SELECT percent_complete FROM sys.dm_exec_requests
- 前端基于jquery的UI框架
正在做的一个项目选择jquery作为前端js核心库.然后就想选一个基于jquery的ui库,然后悲催的事情发生了. 至于为什么使用jquery,一是因为不想为授权费用,而又不想引起可能法律纠纷:另一方 ...
- 异常 Exception 堆栈跟踪 异常捕获 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- Java Base64加密解密
使用Apache commons codec 类Base64 maven依赖 <dependency> <groupId>commons-codec</groupId&g ...
- VS web.config/app.conifg配置文件自定义类型使用智能感知功能
大家使用VS编辑web.config或app.config时可以使用智能感知功能的,像下面这样很是方便 当然如果是我们自定义的类型也是可以使用智能感知的,因为智能感知的内容是来自你或其他公司(MS)提 ...
- colspan width issue
[问] I'm having trouble setting fixed widths on columns which use colspan. It seems that neither IE8, ...
- ZOJ 3456 Traveler Nobita 最小生成树
Traveler Nobita Time Limit: 2 Seconds Memory Limit: 65536 KB One day, Nobita used a time machin ...
- IC卡制作常识概述
ic卡主要有9种: 1.接触型IC卡: 2.非接触型IC卡: 3.串行传输型IC卡: 4.并行传输型IC卡: 5.存储型IC卡: 6.智能型IC卡: 7.超级 ...
- Vim的行号、语法显示等设置(.vimrc文件的配置)以及乱码解决
在终端下使用vim进行编辑时,默认情况下,编辑的界面上是没有显示行号.语法高亮度显示.智能缩进 等功能的.为了更好的在vim下进行工作,需要手动设置一个配置文件:.vimrc.在启动vim时,当前用户 ...