简体   繁体   English

使用石英和弹簧的动态cron作业

[英]Dynamic cron job using quartz+spring

I want to schedule a cron-job using quartz+spring. 我想使用石英+弹簧来安排cron作业。

<context:component-scan base-package="com.hotelscrapper" /> <!--hotelServiceImpl-->
<bean id="ScheduleScrapJob" class="com.hotelscrapper.util.ScheduleScrapJob">
<property name="hotelService" ref="hotelServiceImpl">
</property>
</bean>

public class ScheduleScrapJob extends QuartzJobBean {
private HotelService hotelService;
public void setHotelService(HotelService hotelService){
    this.hotelService = hotelService;
}   
@Override
protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException {
    hotelService.reScrapWorker();
}

}

hotelService is null . hotelService为null I do not understand how i can link hotelService to service hotelServiceImpl . 我不明白如何将hotelService链接到hotelHotelImpl服务。

Solution: hotelService is to be static. 解决方案: hotelService是静态的。 It works fine. 工作正常。

If you using component scanning, you must add the necessary annotations to your classes: 如果使用组件扫描,则必须在类中添加必要的注释:

@Component
public class ScheduleScrapJob extends QuartzJobBean {
  @Autowired
  private HotelService hotelService;

  @Override
  protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException {
    hotelService.reScrapWorker();
  }

} }

With this setup, you will not need the hotel service setter in the ScheduleScrapJob class and your xml configuration will simply be: 使用此设置,您将不需要ScheduleScrapJob类中的酒店服务设置程序,并且您的xml配置将简单地是:

<context:component-scan base-package="com.hotelscrapper" />

This assumes all the classes you need scanned are in the package tree under com.hotelscrapper. 假设您需要扫描的所有类都在com.hotelscrapper下的包树中。

You need to define something like this: 您需要定义如下内容:

<bean id="hotelServiceImpl" class="com.hotelscrapper.HotelServiceImpl">
  <!-- some properties here -->
</bean>

As you did ref="hotelServiceImpl" in the ScheduleScrapJob bean definition, Spring will take this other hotelServiceImpl bean and set it into your ScheduleScrapJob bean, particularly into its hotelService property. 就像您在ScheduleScrapJob bean定义中ref="hotelServiceImpl" ,Spring还将使用另一个hotelServiceImpl bean并将其设置到ScheduleScrapJob bean中,尤其是将其设置为其hotelService属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM