有时候我们需要在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. Java NIO SocketChannel

    A Java NIO SocketChannel is a channel that is connected to a TCP network socket. It is Java NIO's eq ...

  2. 如何查看Isilon的节点的CPU的信息?

    使用Isilon自带的命令 isi_hw_status 使用如下的命令 dmidecode --type processor 笔者只是想了解一下F800的CPU有多少个core.上面的命令都可以获得这 ...

  3. RV LayoutManager 流式布局 MD

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

  4. 【Spark】Sparkstreaming-共享变量-缓存RDD-到底是什么情况?

    Sparkstreaming-共享变量-缓存RDD-到底是什么情况? sparkstreaming 多个 rdd_百度搜索 Spark Streaming中空RDD处理及流处理程序优雅的停止 - xu ...

  5. VIM的buffers

    原文:http://ju.outofmemory.cn/entry/13522 重新在不同的 tab 中打开多个关闭的buffer 文件, https://stackoverflow.com/ques ...

  6. idea缓存目录mac cache

    IDEA如果出现卡顿,Index疯狂扫描,建议清空一下如下目录 ~/Library/Caches/IntelliJIdea2017.3 Resource nexus-maven-repository- ...

  7. Maven hive-jdbc教程

    由于项目中需要用到hive-jdbc从数据仓库拉数据下来,所以简单的学一下hive,hive数据仓库建构在hadoop集群之上,数据存在hdfs文件系统中,hive中执行的操作会装换成mapreduc ...

  8. [R]Kick start

  9. VC++导出具有命名空间的函数

    问题现象 原因分析 解决的方法 1 问题现象 导出具有命名空间的函数和类.源码例如以下: 头文件MiniMFC.h namespace MiniMFC { __declspec(dllexport) ...

  10. android中Snackbar(Design Support)的使用

    Snackbar是Android Design Support Library中的一个组件,想使用Snackbar,必须先引入Design Support,我这里引入的是当前的最新版本: implem ...