上下文是下一个:
我有一个使用Spring 2.5和Struts 1.1的Web应用程序 我使用Quartz在Action中动态创建作业:
JobDetailBean jobDetail = new JobDetailBean();
jobDetail.setBeanName("foo");
Map<String, String> map = new HashMap<String,String>();
map.put("idFeed","foo");
map.put("idSite","foo");
jobDetail.setJobDataAsMap(map);
jobDetail.setJobClass(FeedJob.class);
jobDetail.afterPropertiesSet();
CronTriggerBean cronTrigger = new CronTriggerBean();
cronTrigger.setBeanName("foo");
String expression = " * * * * * *";
cronTrigger.setCronExpression(expression);
cronTrigger.afterPropertiesSet();
// add to schedule
scheduler.scheduleJob((JobDetail) jobDetail, cronTrigger);
scheduler是在Action中注入的org.quartz.Scheduler。
类FeedJob的方法是executeInternal(JobExecutionContext ctx),这是作业必须运行的代码:
public class FeedJob extends QuartzJobBean {
private FeedBL feedBL;
public void setFeedBL(FeedBL feedBL) {this.feedBL = feedBL;}
public FeedJob() {}
public String idFeed;
public String idSite;
public String getIdFeed() {
return idFeed;
}
public void setIdFeed(String idFeed) {
this.idFeed = idFeed;
}
public String getIdSite() {
return idSite;
}
public void setIdSite(String idSite) {
this.idSite = idSite;
}
protected void executeInternal(JobExecutionContext ctx) throws JobExecutionException {
try {
feedBL.sincronizacionProductFeed(idFeed, idSite);
} catch (Exception e) {
e.printStackTrace();
}
}
}
当它要运行时,我在尝试运行这行代码时会得到一个java.lang.NullPointerException:
feedBL.sincronizacionProductFeed(idFeed, idSite);
原因是当我在动作中创建作业时我正在设置作业:
jobDetail.setJobClass(FeedJob.class);
Spring没有注意到他已经创建的bean,所以FeedJob类的实例没有注入feedBL类。
解决这个问题有什么好主意吗? 我试图给这份工作提供这样的背景:
jobDetail.setApplicationContext(applicationContext);
但不起作用。