在quartz的Job中获得Spring的WebApplicationContext或ServletContext

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

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

[java] view plain copy

  1. WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);

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

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

[java] view plain copy

  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. }

重点在

[java] view plain copy

  1. ContextLoader.getCurrentWebApplicationContext();

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

这是spring中关于quartz的配置:

[html] view plain copy

  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的支持):

[html] view plain copy

    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>
时间: 2024-08-29 10:36:30

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

quartz的job中注入spring对象!

一般情况下,quartz的job中使用autowired注解注入的对象为空,这时候我们就要使用spring-quartz提供的AdaptableJobFactory类. 自定义一个类: [java] view plain copy public class JobFactory extends AdaptableJobFactory { @Autowired private AutowireCapableBeanFactory capableBeanFactory; @Override prot

Spring 梳理-webApplicationContext 与servletContext

1.WebApplicationContext的研究 ApplicationContext是spring的核心,Context通常解释为上下文环境,用"容器"来表述更容易理解一些,ApplicationContext则是"应用的容器了"了. spring把bean放在这个容器中,在需要的时候,用getBean()方法取出,在web应用中,会用到webApplicationContext,继承自ApplicationContext 在web.xml初始化WebAppl

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

Spring+quartz集群配置,Spring定时任务集群,quartz定时任务集群 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 蕃薯耀 2016年7月7日 09:06:09 星期四 http://fanshuya

06_在web项目中集成Spring

在web项目中集成Spring 一.使用Servlet进行集成测试 1.直接在Servlet 加载Spring 配置文件 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); HelloService helloService = (HelloService) applicationContext.getBean("helloS

myeclipse中关联spring doc帮助文档

这是一篇分享技巧的文章:使用myeclipse关联帮助文档 ① 选中spring.jar ② 鼠标右击,选择properties,弹出框中选择Javadoc Location,找到对应的文档位置,OK ③ 选择spring.jar中的类 ④ 按住F1,弹出一个help框,选择Java help:Javadoc for 'xxx' ⑤ 下图就是对应的文档内容 myeclipse中关联spring doc帮助文档,码迷,mamicode.com

myeclipse中配置spring xml自动提示

这是一篇分享技巧的文章:myeclipse中配置spring xml自动提示. ① window -> preferences -> MyEclipse -> Files and Editors -> XML -> XML Catalog ② 选择User Specified Entries,点击add按钮弹出一个选框,填入以下三项 i. Location: D:\baiduyun\Spring\spring_doc\soft\spring-framework-2.5.6\d

在Eclipse mars 4.5.2 中安装spring 插件 spring tool suite

最近在学习spring,用到的IDE 有eclipse,也有用到 IDEA. 目前对spring还不是很了解,跟着网上的视频来,先学会了spring,然后再选IDE. 题归正转,下面说说怎么在Eclipse mars 4.5.2 中安装spring 插件 spring tool suite. 打开eclipse,然后在菜单栏中点击Help,选中Eclipse MarketPlace: 然后搜索STS(也就是spring tool suite的缩写),回车: 点击Install即可 由于是在线安装

step4-----&gt;往工程中添加Spring的子项目spring IO Platform-------&gt;通过maven添加相关框架(pom.xml)

添加Spring IO Platform的目的: 避免自己的project的外部依赖(external dependencies)之间产生版本冲突问题.更多详细信息参见:Spring IO Platform概述 具体操作步骤: step1,往自己的工程中添加Spring IO Platform 编写project的pom.xml,添加如下代码,引入Spring IO Platform <dependencyManagement> <dependencies> <depende

step4---&gt;往工程中添加Spring框架----&gt;修改maven的配置文件pom.xml,向工程中添加spring框架的某些模块

1.本文内容: 本文介绍使用maven向自己的项目中添加各种框架的方法,即如何配置maven的pom.xml来让maven帮助管理这些框架(包括Spring.SpringMVC.hibernate框架等等). 2.使用maven向自己的工程中添加框架: 2.1概述 若想使用maven向自己的工程中添加三方框架(如Spring.SpringMVC等),需要先确保你的工程是maven工程,如果你还不知道该如何在myeclipse中建立一个maven web project,请参考相关教程. 2.2使