spring3.1的BeanFactory与Quartz1.8整合

spring的applicationContext.xml配置文件:

加入

<bean id="myJob" class="org.springframework.scheduling.quartz.JobDetailBean">
  <property name="jobClass">
   <value>com.job.Service</value>
  </property>
 </bean> 
 <bean id="myCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
  <property name="jobDetail">
   <ref bean="myJob"/>
  </property>
  <property name="cronExpression"> 
   <value>0 0 1 * * ?</value>
  </property>
 </bean>
 <bean id="schedulerFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> 
  <property name="jobFactory"> 
   <bean class="com.JobFactory"/> 
  </property> 
  <property name="triggers"> 
   <list> 
   <ref bean="myCronTrigger"/> 
   </list> 
  </property> 
 </bean>

 

quartz与spring的BeanFactory整合的类: 

/**
 * 为定时器提供自动注入功能
 */
public class JobFactory extends SpringBeanJobFactory implements
		ApplicationContextAware {

	private ApplicationContext applicationContext;

	@Override
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		this.applicationContext = applicationContext;
	}

	@Override
	protected Object createJobInstance(TriggerFiredBundle bundle)
			throws Exception {
		Object jobInstance = super.createJobInstance(bundle);
		applicationContext.getAutowireCapableBeanFactory().autowireBean(	jobInstance);
		return jobInstance;
	}
	
}

 

 定时任务类:

public class MyService extends QuartzJobBean {

	private static final Log log = LogFactory.getLog(MyService.class);
	
	@Resource
	private UserService userService;
	
	@Override
	protected void executeInternal(JobExecutionContext jobExecutionContext)
			throws JobExecutionException {
		log.info("执行定时任务");
		userService.freshUserAmount();
	}
	
}

 

Quartz中时间表达式的设置-----corn表达式 

时间格式: <!-- s m h d m w(?) y(?) -->,   分别对应: 秒>分>小时>日>月>周>年, 

posted on 2014-02-11 11:25  zhaofeng555  阅读(340)  评论(0编辑  收藏  举报