有时候我们需要在web工程中定时器类里面获得spring的IOC容器,即WebApplicationContext,用它来获取实现了某接口的所有的bean,因为@Autowired貌似只能注入单个bean。

一开始我是写的一个ServletContextListener,启动服务器的时候就构造定时器并启动,把WebApplicationContext传给定时器的Job,在ServletContextListener中这样得到WebApplicationContext:

  1. WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);

然后在Job中调用webApplicationContext.getBeansOfType(InfoService.class) 得到实现接口的所有bean。

其实,可以更简单,废话少说,这是一个POJO的Job:

  1. package com.gxjy.job;
  2. import java.util.Map;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.web.context.ContextLoader;
  5. import org.springframework.web.context.WebApplicationContext;
  6. import com.gxjy.dao.InfoDao;
  7. import com.gxjy.service.InfoService;
  8. import com.gxjy.service.runnable.DudeRunner;
  9. public class ScrawlerJob{
  10. @Autowired
  11. private InfoDao infoDao;
  12. public void execute() {
  13. WebApplicationContext  wac = ContextLoader.getCurrentWebApplicationContext();
  14. Map<String, InfoService>  map = wac.getBeansOfType(InfoService.class);
  15. for (InfoService infoService : map.values()) {
  16. System.out.println("启动:"+infoService.getClass().getName());
  17. new Thread(new DudeRunner(infoService, infoDao)).start();
  18. }
  19. }
  20. }

重点在

  1. ContextLoader.getCurrentWebApplicationContext();

这个可以直接获取WebApplicationContext,当然还可以进一步调用getServletContext()就获取到ServletContext了。

这是spring中关于quartz的配置:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5. <bean id="job" class="com.gxjy.job.ScrawlerJob"></bean>
  6. <bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
  7. <property name="targetObject">
  8. <ref bean="job"/>
  9. </property>
  10. <property name="targetMethod">
  11. <value>execute</value>
  12. </property>
  13. </bean>
  14. <bean id="trigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
  15. <property name="jobDetail">
  16. <ref bean="jobDetail"/>
  17. </property>
  18. <property name="cronExpression">
  19. <value>0 0 3 * * ?</value>
  20. </property>
  21. </bean>
  22. <bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
  23. <property name="triggers">
  24. <list>
  25. <ref bean="trigger"/>
  26. </list>
  27. </property>
  28. <property name="autoStartup" value="true"></property>
  29. </bean>
  30. </beans>

maven依赖除了基本的spring和quartz之外还需要加入spring-context-support的依赖(包含对quartz的支持):

    1. <pre name="code" class="html">    <dependency>
    2. <groupId>org.springframework</groupId>
    3. <artifactId>spring-context-support</artifactId>
    4. <version>4.2.2.RELEASE</version>
    5. </dependency>

在quartz的Job中获得Spring的WebApplicationContext或ServletContext的更多相关文章

  1. quartz的job中注入spring对象!

    一般情况下,quartz的job中使用autowired注解注入的对象为空,这时候我们就要使用spring-quartz提供的AdaptableJobFactory类. 自定义一个类: public  ...

  2. Spring 梳理-webApplicationContext 与servletContext

    1.WebApplicationContext的研究 ApplicationContext是spring的核心,Context通常解释为上下文环境,用“容器”来表述更容易理解一些,Applicatio ...

  3. 从servlet中获取spring的WebApplicationContext

    需要做一个参数初始化类,当web应用被加载时从数据库里取出相关的参数设置 ,并把这些参数放置到application里,jsp页面可以从中取出. 1.在web.xml中配置: <servlet& ...

  4. 在Servlet中获取spring容器WebApplicationContext

    WebApplicationContext springContext = WebApplicationContextUtils.getRequiredWebApplicationContext(ge ...

  5. Spring+quartz集群配置,Spring定时任务集群,quartz定时任务集群

    Spring+quartz集群配置,Spring定时任务集群,quartz定时任务集群 >>>>>>>>>>>>>> ...

  6. 信步漫谈之Quartz—分布式调度(整合spring早期版本【低于spring3.1】)

    一.环境 使用的jar包:spring2.5.6.quartz1.8.6 二.注意点 因为spring内置的quartz版本变化,所以存在spring和quartz版本接口兼容情况,如下: 1)spr ...

  7. quartz的job怎么获取Spring上下文

    第一步.在org.springframework.scheduling.quartz.SchedulerFactoryBean对象中注入applicationContextSchedulerConte ...

  8. 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架构模式,可 ...

  9. step6----->往工程中添加spring boot项目------->修改pom.xml使得我的project是基于spring boot的,而非直接基于spring framework

    文章内容概述: spring项目组其实有多个projects,如spring IO platform用于管理external dependencies的版本,通过定义BOM(bill of mater ...

随机推荐

  1. Asp.net WebAPi gzip压缩和json格式化

    现在webapi越来越流行了,很多时候它都用来做接口返回json格式的数据,webapi原本是根据客户端的类型动态序列化为json和xml的,但实际很多时候我们都是序列化为json的,所以webapi ...

  2. 轻松搞定 easyui datagrid 二次加载的问题(转)

    对于使用url方式的初学者,经常碰到重复请求的问题,这个问题的根源是因为一旦设置了url参数,Datagrid组件在实例化的时候就会做请求,如何避免二次加载这样问题呢,个人觉得注意以下两点基本就可以防 ...

  3. 你应该知道的Linux历史

    说道linux的历史不得不说的就是unix,我们的linux就是类unix系统: 1969年第一台unix系统 贝尔实验室 1970年C语言诞生,对unix内核重新编写 system V(AT& ...

  4. css的overflow属性

    原文:https://www.jianshu.com/p/67b536fc67c1 ------------------------------------------- 事实上我挺长一段时间都没弄清 ...

  5. Google声明机器学习在自己定制的芯片比方普通的GPU和CPU快15到30倍

    GOOGLE开发自己的加速机器学习的芯片已经不是什么秘密了,最先发布出来的是TPU(Tensor Processing Units),在2016年5月I/O开发大会上发布的.可是没有发布相关的细节情况 ...

  6. oauth2-server-php-docs 概念

    PHP的OAuth2服务器库 将OAuth2.0干净地安装到您的PHP应用程序中. 从GitHub 下载代码开始. 要求 这个库需要PHP 5.3.9+.然而,有一个稳定的版本和开发分支的PHP 5. ...

  7. [Canvas]奔跑的马

    下载地址:https://files.cnblogs.com/files/xiandedanteng/52-WalkingHorse.rar,请用Chrome浏览器打开观看动态效果. 图例: 源码: ...

  8. redis 安装报错

    CentOS 6.5 安装 Redis 执行 make #error "Newer version of jemalloc required" 根据你系统安装时或之后安装的选项的情 ...

  9. 分布式锁与实现(一)——基于Redis实现(转载)

    php的完整流程,包护队列操作:http://www.cnblogs.com/candychen/p/5736128.html 概述 目前几乎很多大型网站及应用都是分布式部署的,分布式场景中的数据一致 ...

  10. 庞果英雄会第二届在线编程大赛·线上初赛:AB数

    题目链接 给定两个正整数a,b,分别定义两个集合L和R, 集合L:即把1~a,1~b中整数乘积的集合定义为L = {x * y | x,y是整数且1 <= x <=a , 1 <= ...