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

來源:互聯網
上載者:User

標籤:cto   cti   pre   tar   調用   www.   beans   att   etc   

有時候我們需要在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.   
  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. }  


重點在

[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.       
  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的支援):

[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>  

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.