我使用Spring 4.0并将项目从xml移动到java-config,除了从@Service("scheduleService")
访问QuartzJobBean.executeInternal
带注释的类外,一切正常。
我必须让它工作的xml位是:
<bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="schedulerContextAsMap">
<map>
<entry key="scheduleService" value-ref="scheduleService" />
<entry key="knxUtil" value-ref="knxUtil" />
</map>
</property>
</bean>
然后,为了安排我使用的@Service("scheduleService")
内的作业:
JobBuilder jobBuilder = JobBuilder.newJob(ScheduledActionRunner.class)
此外,为了实际执行Job
,我让它像这样工作:
@Component
public class ScheduledActionRunner extends QuartzJobBean {
private KNXUtil knxUtil;
private ScheduleService scheduleService;
public ScheduledActionRunner() {
}
@Autowired
public void setScheduleService(ScheduleService scheduleService) {
this.scheduleService = scheduleService;
}
@Autowired
public void setKnxUtil(KNXUtil knxUtil) {
this.knxUtil = knxUtil;
}
@Override
public void executeInternal(JobExecutionContext context) throws JobExecutionException {
JobDataMap jobDataMap = context.getMergedJobDataMap();
String scheduleId = jobDataMap.getString("scheduleId");
Schedule schedule = scheduleService.get(scheduleId);
Set<ScheduledAction> actions = schedule.getScheduledActions();
for (ScheduledAction scheduledAction : actions) {
scheduledAction.getAction().execute(logger, knxUtil);
}
}
如上所述,使用xml-configuration时所有这些都可以工作。
现在,使用java-config,它在NullPointerException
scheduleService.get(scheduleId);
对于java配置,我设置了SchedulerFactoryBean
,如下所示:
@Configuration
@PropertySource(value = "classpath:properties.${target_env:dev}.properties")
@ComponentScan(basePackages = { "com.example.smart" }
public class SpringRootApplication {
@Autowired
private ScheduleService scheduleService;
@Autowired
private KNXUtil knxUtil;
@Bean
SchedulerFactoryBean schedulerFactoryBean() {
SchedulerFactoryBean bean = new SchedulerFactoryBean();
Map<String, Object> schedulerContextAsMap = new HashMap<String, Object>();
schedulerContextAsMap.put("scheduleService", scheduleService);
schedulerContextAsMap.put("knxUtil", knxUtil);
bean.setSchedulerContextAsMap(schedulerContextAsMap);
return bean;
}
}
如何使用java-config在scheduleService
内插入schedulerContextAsMap
的引用?
答案 0 :(得分:2)
我在这里找到答案:https://stackoverflow.com/a/17394905/272180
解决方案是在设置bean.setSchedulerContextAsMap(schedulerContextAsMap)
时删除SchedulerFactoryBean
,并按如下方式修改executeInternal
:
@Autowired
private KNXUtil knxUtil;
@Autowired
private ScheduleService scheduleService;
@Override
public void executeInternal(JobExecutionContext context) throws JobExecutionException {
// Adding this autowires everything as needed
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
JobDataMap jobDataMap = context.getMergedJobDataMap();
String scheduleId = jobDataMap.getString("scheduleId");
Schedule schedule = scheduleService.get(scheduleId);
Set<ScheduledAction> actions = schedule.getScheduledActions();
for (ScheduledAction scheduledAction : actions) {
scheduledAction.getAction().execute(logger, knxUtil);
}
}
这也简化了代码,因为不再需要@Autowired
带注释的setter。