我使用 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>
然后,为了安排里面的Job@Service("scheduleService")
I used:
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 配置时起作用。
现在,使用 java-config,它失败了NullPointerException
at 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;
}
}
我怎样才能插入参考scheduleService
在 - 的里面schedulerContextAsMap
使用java配置?