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

有时候我们需要在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.   
  3. import java.util.Map;  
  4.   
  5. import org.springframework.beans.factory.annotation.Autowired;  
  6. import org.springframework.web.context.ContextLoader;  
  7. import org.springframework.web.context.WebApplicationContext;  
  8.   
  9. import com.gxjy.dao.InfoDao;  
  10. import com.gxjy.service.InfoService;  
  11. import com.gxjy.service.runnable.DudeRunner;  
  12.   
  13. public class ScrawlerJob{  
  14.       
  15.     @Autowired  
  16.     private InfoDao infoDao;  
  17.   
  18.     public void execute() {  
  19.         WebApplicationContext  wac = ContextLoader.getCurrentWebApplicationContext();  
  20.         Map<String, InfoService>  map = wac.getBeansOfType(InfoService.class);  
  21.         for (InfoService infoService : map.values()) {  
  22.             System.out.println("启动:"+infoService.getClass().getName());  
  23.             new Thread(new DudeRunner(infoService, infoDao)).start();  
  24.         }  
  25.     }  
  26.   
  27. }  


重点在

  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.       
  6.     <bean id="job" class="com.gxjy.job.ScrawlerJob"></bean>  
  7.       
  8.       
  9.     <bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">  
  10.         <property name="targetObject">  
  11.             <ref bean="job"/>  
  12.         </property>  
  13.         <property name="targetMethod">  
  14.             <value>execute</value>  
  15.         </property>  
  16.     </bean>  
  17.       
  18.       
  19.     <bean id="trigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">  
  20.         <property name="jobDetail">  
  21.             <ref bean="jobDetail"/>  
  22.         </property>  
  23.         <property name="cronExpression">  
  24.             <value>0 0 3 * * ?</value>  
  25.         </property>  
  26.     </bean>  
  27.       
  28.       
  29.     <bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  
  30.         <property name="triggers">  
  31.             <list>  
  32.                 <ref bean="trigger"/>  
  33.             </list>  
  34.         </property>  
  35.         <property name="autoStartup" value="true"></property>  
  36.     </bean>  
  37.       
  38. </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>  
原文地址:https://www.cnblogs.com/onetwo/p/7279091.html