zoukankan      html  css  js  c++  java
  • spring quartz整合实现定时器自动注解

    1.web.xml中添加侦听器

      <listener>
          <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
        </listener>

    2.提供自动注解实现类

      /**
      * Copyright 2011-2013 Brin.com
      * All rights reserved.
      *
      * @project
      * @author Brin
      * @version 1.0
      * @data 2013-10-10
      */
      package com.brin.core.common.quartz;

      import org.quartz.spi.TriggerFiredBundle;
      import org.springframework.beans.BeansException;
      import org.springframework.context.ApplicationContext;
      import org.springframework.context.ApplicationContextAware;
      import org.springframework.scheduling.quartz.SpringBeanJobFactory;

      /**
      * @description 为定时器提供自动注入功能
      * @author Brin
      *
      */
      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;
        }
      }

    3.quartz.xml中配置jobFactory

      <beans:bean id="schedulerFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <beans:property name="jobFactory">
          <beans:bean class="com.brin.core.common.quartz.JobFactory"/>
        </beans:property>
        <beans:property name="triggers">
          <beans:list>
            <beans:ref bean="remindersTimer"/>
          </beans:list>
        </beans:property>
      </beans:bean>

    定时器中的@Autowired注入的类,可以实例化

  • 相关阅读:
    让人头疼的CSS兼容
    javascript IE与其他主流浏览器兼容性问题积累
    webpack全局安装后,提示webpack命令不可用的解决方法
    Vue Devtools的安装
    box-sizing属性
    margin-left和left的区别
    position定位解决弹框拖拽出屏幕的情况
    css单位的px,em,rem的区别总结笔记
    用css3的属性transform画一个太阳
    CSS高级技巧-转自51cto
  • 原文地址:https://www.cnblogs.com/Jiphen/p/3361765.html
Copyright © 2011-2022 走看看