1. 程式人生 > >在quartz的Job中獲得Spring的WebApplicationContext或ServletContext

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

listen package load str tcl imp attribute utf ger

有時候我們需要在web工程中定時器類裏面獲得spring的IOC容器,即WebApplicationContext,用它來獲取實現了某接口的所有的bean,[email protected]

一開始我是寫的一個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>

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