Spring+Hibernate+Struts2整合Quartz, JOB中无法使用Spring注入

转载自:http://liulijun-cn-2011.iteye.com/blog/1597648   

      Quartz线程不是一个HTTP请求,不包含request对象,在Quartz的Job类中无法取得ServletContext,
所以无法取得ApplicationContext,Job类中的Spring注解都是无效的。现在通过定义一个静态的变量ServletContext在服务器容器启动的时候,实例化ServletContext。

所以无法取得ApplicationContext,Job类中的Spring注解都是无效的。现在通过定义一个静态的变量ServletContext在服务器容器启动的时候,实例化ServletContext。  


ContextLoaderListener: 
Java代码   收藏代码
  1. public class ContextLoaderListener implements ServletContextListener {  
  2.     /** 
  3.      * @see ServletContextListener#contextInitialized(ServletContextEvent) 
  4.      */  
  5.     public void contextInitialized(final ServletContextEvent event) {  
  6.         String appRoot = event.getServletContext().getRealPath("/");  
  7.   
  8.         AppContext.init(appRoot);  
  9.   
  10.         QuartzContext.getInstance().setContext(event.getServletContext());  
  11.     }  
  12.   
  13.     /** 
  14.      * @see ServletContextListener#contextDestroyed(ServletContextEvent) 
  15.      */  
  16.     public void contextDestroyed(final ServletContextEvent event) {  
  17.         // do nothing  
  18.     }  
  19. }  


Web.xml 
Xml代码   收藏代码
  1. <listener>  
  2.        <listener-class>  
  3.             jp.xx.xxx.xxxxx.xxx.servlet.listener.ContextLoaderListener  
  4.        </listener-class>  
  5. </listener>  


QuartzContext: 
Java代码   收藏代码
  1. public class QuartzContext {  
  2.     private final static QuartzContext instance = new QuartzContext ();  
  3.     private static ServletContext context;  
  4.   
  5.     private QuartzContext () {  
  6.     }  
  7.   
  8.     public static QuartzContext getInstance() {  
  9.         return instance;  
  10.     }  
  11.   
  12.     protected void setContext(final ServletContext context) {  
  13.         context = context;  
  14.     }  
  15.   
  16.     public ServletContext getContext() {  
  17.         return context;  
  18.     }  
  19. }  


XML设定: 
Xml代码   收藏代码
  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" xmlns:p="http://www.springframework.org/schema/p"  
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6.     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  7.     http://www.springframework.org/schema/context  
  8.     http://www.springframework.org/schema/context/spring-context-3.0.xsd">  
  9.   
  10.   
  11.     <bean name="jobDetail" class="org.springframework.scheduling.quartz.JobDetailBean"  
  12.         p:jobClass="jp.xx.xxx.xxxxx.xxx.quartz.base.MyJob"  
  13.         p:applicationContextJobDataKey="applicationContext">  
  14.     </bean>  
  15.   
  16.   
  17.     <bean id="checkImagesTrigger"  
  18.         class="org.springframework.scheduling.quartz.CronTriggerBean"  
  19.         p:jobDetail-ref="jobDetail"  
  20.         p:cronExpression="0 0 0 1 */3 ?">  
  21.         <property name="jobDataAsMap">  
  22.             <map>  
  23.                 <entry key="name" value="checkImagesTrigger" />  
  24.             </map>  
  25.         </property>  
  26.     </bean>  
  27.   
  28.     <bean id="checkImagesTrigger2"  
  29.         class="org.springframework.scheduling.quartz.CronTriggerBean"  
  30.         p:jobDetail-ref="jobDetail"  
  31.         p:cronExpression="10/10 * * * * ?">  
  32.         <property name="jobDataAsMap">  
  33.             <map>  
  34.                 <entry key="name" value="checkImagesTrigger2" />  
  35.             </map>  
  36.         </property>  
  37.     </bean>  
  38.   
  39.     <bean id="scheduler" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">  
  40.         <property name="triggers">  
  41.             <list>  
  42.                 <ref bean="checkImagesTrigger" />  
  43.                 <ref bean="checkImagesTrigger2" />  
  44.             </list>  
  45.         </property>  
  46.         <property name="schedulerContextAsMap">  
  47.             <map>  
  48.                 <entry key="timeout" value="30" />  
  49.             </map>  
  50.         </property>  
  51.         <property name="configLocation" value="classpath:/config/quartz/quartz.properties" />  
  52.     </bean>  
  53.   
  54. </beans>  


MyJob: 
Java代码   收藏代码
  1. public class MyJob extends BaseServiceImpl implements Job {  
  2.   
  3.     public void execute(JobExecutionContext jctx) throws JobExecutionException {  
  4.         ServletContext scontext = QuartzContext.getInstance().getContext();  
  5.         ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(scontext);  
  6.         SysUserService sysUserService = (SysUserService)ctx.getBean("sysUserService");  
  7.   
  8.         SysUser sysUser = sysUserService.getSysUser(1);  
  9.   
  10.         System.out.println(sysUser.getName());  
  11.     }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值