我正在使用Quartz和Spring Framework重写遗留项目。原始配置是XML格式,现在我将其翻译成Java Config。 xml配置使用jobDetail设置触发器bean的作业详细信息属性。但是,当我尝试使用等效方法,即setter:setJobDetails(simpleJobDetail)时,我收到一个警告,表明setter没有正确的类型(期望JobDetails,但得到了MethodInvokingJobDetailFactoryBean)。
我是否可以通过在Java COnfig中使用等效的命名setter将xml bean配置转换为Java COnfig是否正确?
为什么在XML属性设置中,触发器bean可以将其jobDetail属性设置为simpleJobDetail bean(其类型为MethodInvokingJobDetailFactoryBean),而Java Config则不能?
XML配置:
<!-- For times when you just need to invoke a method on a specific object -->
<bean id="simpleJobDetail"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="reader" />
<property name="targetMethod" value="readData" />
</bean>
<!-- Run the job every 60 seconds with initial delay of 1 second -->
<bean id="trigger"
class="org.springframework.scheduling.quartz.SimpleTriggerFactoryBean">
<property name="jobDetail" ref="simpleJobDetail" />
<property name="repeatInterval" value="600000" />
</bean>
Java配置:
@Bean
public MethodInvokingJobDetailFactoryBean simpleJobDetail() {
MethodInvokingJobDetailFactoryBean simpleJobDetail = new MethodInvokingJobDetailFactoryBean();
simpleJobDetail.setTargetObject(reader());
simpleJobDetail.setTargetMethod("readData");
return simpleJobDetail;
}
@Bean
private Object reader() {
// TODO: 10/13/2016
return null;
}
@Bean
public SimpleTriggerFactoryBean trigger() {
final SimpleTriggerFactoryBean trigger = new SimpleTriggerFactoryBean();
trigger.setJobDetail( simpleJobDetail()); // got warning about wrong type
trigger.setRepeatInterval(60000);
return trigger;
}
答案 0 :(得分:1)
请注意,simpleJobDetail()返回工厂,而不是bean本身。您可以依靠自动装配来注入使用此工厂构建的JobDetail。
@Bean
public SimpleTriggerFactoryBean trigger(final JobDetail simpleJobDetail) {
final SimpleTriggerFactoryBean trigger = new SimpleTriggerFactoryBean();
trigger.setJobDetail(simpleJobDetail); // got warning about wrong type
trigger.setRepeatInterval(60000);
return trigger;
}
希望它有所帮助。