有时候我们需要在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. 【未解决】centos 6.4 xen4.2 在关机的时候很慢

    centos xen 在关机的时候 下面的关闭DomUs虚拟机 耗时很长... stopping xenconsoled daemon: [OK] sending shutdown to all Do ...

  2. 查看sql执行的情况

    今天同事让看一个sql,无意中学到一个查看sql执行情况的命令,以前都是傻傻的等的,今后不用了 SELECT percent_complete FROM sys.dm_exec_requests

  3. 前端基于jquery的UI框架

    正在做的一个项目选择jquery作为前端js核心库.然后就想选一个基于jquery的ui库,然后悲催的事情发生了. 至于为什么使用jquery,一是因为不想为授权费用,而又不想引起可能法律纠纷:另一方 ...

  4. 异常 Exception 堆栈跟踪 异常捕获 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  5. Java Base64加密解密

    使用Apache commons codec 类Base64 maven依赖 <dependency> <groupId>commons-codec</groupId&g ...

  6. VS web.config/app.conifg配置文件自定义类型使用智能感知功能

    大家使用VS编辑web.config或app.config时可以使用智能感知功能的,像下面这样很是方便 当然如果是我们自定义的类型也是可以使用智能感知的,因为智能感知的内容是来自你或其他公司(MS)提 ...

  7. colspan width issue

    [问] I'm having trouble setting fixed widths on columns which use colspan. It seems that neither IE8, ...

  8. ZOJ 3456 Traveler Nobita 最小生成树

    Traveler Nobita Time Limit: 2 Seconds      Memory Limit: 65536 KB One day, Nobita used a time machin ...

  9. IC卡制作常识概述

    ic卡主要有9种:    1.接触型IC卡:    2.非接触型IC卡:    3.串行传输型IC卡:    4.并行传输型IC卡:    5.存储型IC卡:    6.智能型IC卡:    7.超级 ...

  10. Vim的行号、语法显示等设置(.vimrc文件的配置)以及乱码解决

    在终端下使用vim进行编辑时,默认情况下,编辑的界面上是没有显示行号.语法高亮度显示.智能缩进 等功能的.为了更好的在vim下进行工作,需要手动设置一个配置文件:.vimrc.在启动vim时,当前用户 ...