问题描述
我使用 Spring 4.0 并将项目从 xml 移动到 java-config,除了访问 QuartzJobBean.executeInternal
中的 @Service("scheduleService")
注释类外,一切正常代码>.
I use Spring 4.0 and have moved a project from xml to java-config, and everything works except accessing an @Service("scheduleService")
annotated class from QuartzJobBean.executeInternal
.
我必须使其工作的 xml 位是:
The xml-bit I had to make it work was:
<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")
内调度作业,我使用了:
Then, for scheduling the Job inside @Service("scheduleService")
I used:
JobBuilder jobBuilder = JobBuilder.newJob(ScheduledActionRunner.class)
此外,为了实际执行Job
,我是这样工作的:
Furthermore, to actually execute the Job
I had it working like this:
@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 时起作用.
As mentioned above, all this used to work while using xml-configuration.
现在,使用 java-config,它在 scheduleService.get(scheduleId);
Now, with java-config, it fails with NullPointerException
at scheduleService.get(scheduleId);
对于 java 配置,我像这样设置 SchedulerFactoryBean
:
For java-configuration I set up the SchedulerFactoryBean
like this:
@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 在 schedulerContextAsMap
中插入对 scheduleService
的引用?
How can I insert the reference to scheduleService
inside the schedulerContextAsMap
using java-config?
推荐答案
我在这里找到了答案:https://stackoverflow.com/a/17394905/272180
解决办法是在设置SchedulerFactoryBean
时去掉bean.setSchedulerContextAsMap(schedulerContextAsMap)
,修改executeInternal
如下:
The solution was to remove bean.setSchedulerContextAsMap(schedulerContextAsMap)
when setting up SchedulerFactoryBean
, and modify executeInternal
as follows:
@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.
This also simplifies the code since the @Autowired
annotated setters are not needed anymore.
这篇关于如何在 javaconfig 中使用 SchedulerFactoryBean.schedulerContextAsMap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!