代码之家  ›  专栏  ›  技术社区  ›  Mahmoud Saleh

如何在石英作业中使用@autowired?

  •  32
  • Mahmoud Saleh  · 技术社区  · 14 年前

    我用石英和弹簧 我想加入/使用工作类中的另一个类 我不知道该怎么做

    XML:

    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
    
       <!-- Scheduler task -->
       <bean name="schedulerTask" class="com.mkyong.quartz.SchedulerTask" />
    
       <!-- Scheduler job -->
       <bean name="schedulerJob"
           class="org.springframework.scheduling.quartz.JobDetailBean">
    
         <property name="jobClass" value="com.mkyong.quartz.SchedulerJob" />
    
         <property name="jobDataAsMap">
            <map>
              <entry key="schedulerTask" value-ref="schedulerTask" />
             </map>
          </property>
       </bean>
    
       <!-- Cron Trigger -->
       <bean id="cronTrigger"
        class="org.springframework.scheduling.quartz.CronTriggerBean">
    
        <property name="jobDetail" ref="schedulerJob" />
        <property name="cronExpression" value="0/10 * * * * ?" />
    
       </bean>
    
       <!-- Scheduler -->
       <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="jobDetails">
           <list>
              <ref bean="schedulerJob" />
           </list>
        </property>
    
        <property name="triggers">
            <list>
            <ref bean="cronTrigger" />
            </list>
        </property>
       </bean>
    
    </beans>
    

    石英工作:

    package com.mkyong.quartz;
    
    import org.quartz.JobExecutionContext;
    import org.quartz.JobExecutionException;
    import org.springframework.scheduling.quartz.QuartzJobBean;
    
    public class SchedulerJob extends QuartzJobBean
    {
        private SchedulerTask schedulerTask;
    
        public void setSchedulerTask(SchedulerTask schedulerTask) {
            this.schedulerTask = schedulerTask;
        }
    
        protected void executeInternal(JobExecutionContext context)
        throws JobExecutionException {
    
            schedulerTask.printSchedulerMessage();
    
        }
    }
    

    要执行的任务:

    package com.mkyong.quartz;
    
    public class SchedulerTask {
    
       public void printSchedulerMessage() {
    
           System.out.println("Struts 2 + Spring + Quartz ......");
    
       }
    }
    

    我想在任务类中插入另一个处理数据库的DTO类 要在任务中做一些数据库工作,如何做?

    5 回复  |  直到 8 年前
        1
  •  10
  •   Grzegorz Oledzki    14 年前

    不确定这是否是您想要的,但是您可以将一些配置值传递给Quartz作业。我相信你可以利用 jobDataAsMap 您已经设置的属性,例如:

     <property name="jobDataAsMap">
        <map>
          <entry key="schedulerTask" value-ref="schedulerTask" />
          <entry key="param1" value="com.custom.package.ClassName"/>
         </map>
      </property>
    

    然后,您应该能够以手动方式访问它的实际Java代码:

    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        schedulerTask.printSchedulerMessage();
        System.out.println(context.getJobDetail().getJobDataMap().getString("param1"));
    }
    

    或者使用神奇的弹簧方法 param1 用getter/setter定义的属性。你可以试着定义它 java.lang.Class 然后键入并自动完成(Spring会为您执行此操作):

     private Class<?> param1;
    
     // getter & setter
    
     protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        schedulerTask.printSchedulerMessage();
        System.out.println("Class injected" + getParam1().getName());
     }     
    

    不过我还没有测试过。

        2
  •  14
  •   Kieren Dixon    13 年前

    在您的解决方案中,您使用的是Spring@autowired注释,它不是由Spring实例化的类。如果删除@autowired注释,您的解决方案仍然有效,因为Quartz正在设置属性,而不是Spring。

    Quartz将尝试将JobDatamap中的每个键设置为属性。例如,由于您有一个键“mydao”,Quartz将查找一个名为“setmydao”的方法,并将该键的值传递给该方法。

    如果您希望Spring将Spring Beans注入到您的工作中,请创建一个SpringBeanJobFactory,并将其设置到您的时间表FactoryBean中。 工作工厂 您的Spring上下文中的属性。

    SpringBeanJobFactory JavaDoc公司:

    应用计划程序上下文、作业数据映射和触发器数据映射项 作为bean属性值

        3
  •  9
  •   Tomik    12 年前
    ApplicationContext springContext = 
        WebApplicationContextUtils.getWebApplicationContext(
            ContextLoaderListener.getCurrentWebApplicationContext().getServletContext()
        );
    Bean bean = (Bean) springContext.getBean("beanName");
    bean.method();
    
        4
  •  3
  •   Community    7 年前

    如中所述 inject bean reference into a Quartz job in Spring? 你可以用弹簧 SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);

    @Named
    public class SampleJob implements Job {
    
        @Inject
        private AService aService;
    
       @Override
        public void execute(JobExecutionContext context)
            throws JobExecutionException {
    
           //Do injection with spring
            SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
            aService.doIt();
           }
    }
    

    如前所述,它可能不会在某些Spring版本上工作,但我已经在4.2.1.版本上进行了测试,这个版本工作得很好。

        5
  •  0
  •   martin    8 年前

    这是我的解决方案:

        public class MySpringBeanJobFactory extends
            org.springframework.scheduling.quartz.SpringBeanJobFactory implements
            ApplicationContextAware {
          private ApplicationContext ctx;
    
        @Override
        public void setApplicationContext(ApplicationContext applicationContext)
                throws BeansException {
            this.ctx = applicationContext;
        }
    
    
        @Override
        protected Object createJobInstance(TriggerFiredBundle bundle)
                throws Exception {
    
            Object jobInstance = super.createJobInstance(bundle);
            ctx.getAutowireCapableBeanFactory().autowireBean(jobInstance);
            return jobInstance;
        }
    }
    

    然后在XML中配置myspringBeanJobFactory的类

    <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
            <property name="jobFactory">
                <bean class="com.xxxx.MySpringBeanJobFactory" />
        </property>
            <property name="configLocation" value="classpath:quartz.properties" />
            <property name="triggers">
                <list>
                    <ref bean="cronTrigger"/>
                </list>
            </property>
        </bean>
    

    祝你好运!:)