Description
m-kay opened SPR-17323 and commented
When configuring a Quartz JobDetail with a bean which does not have a default constructor but only a constructor with argumnets to be injected the AutowireCapableBeanJobFactory fails to create the job. When removing the parameters from the constructor and use field injection everything works fine, however field injection is not recommended.
I'm using spring boot 2.0.5 with the starter spring-boot-starter-quartz and my configuration looks like following:
@Configuration
public class QuartzConfig {
@Bean(name = "myQuartzJobDetail")
public JobDetailFactoryBean myQuartzJobDetail() {
JobDetailFactoryBean jobDetailFactory = new JobDetailFactoryBean();
jobDetailFactory.setJobClass(MyJob.class);
jobDetailFactory.setDurability(true);
return jobDetailFactory;
}
@Bean
public CronTriggerFactoryBean advertisementUpdaterTrigger(@Qualifier("myQuartzJobDetail") JobDetail job) {
CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();
cronTriggerFactoryBean.setCronExpression("0 0/1 * * * ? *");
cronTriggerFactoryBean.setJobDetail(job);
return cronTriggerFactoryBean;
}
}
MyJob which does not work
@Component
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
class MyJob extends QuartzJobBean {
private SomeService someService;
@Autowired
public MyJob(SomeService someService) {
this.someService = someService;
}
protected void executeInternal(JobExecutionContext context){
System.out.println("data from service " + someService.getData());
}
}
MyJob which works
@Component
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
class MyJob extends QuartzJobBean {
@Autowired
private SomeService someService;
protected void executeInternal(JobExecutionContext context){
System.out.println("data from service " + someService.getData());
}
}
In my opinion the job factory should not create a new instance but rather get the instance from the application context and let the context create the instance if the scope is set to prototype.
Issue Links:
- Ability to suppress "rejectedValue" in error responses [SPR-14771] #19337 Ability to suppress "rejectedValue" in error responses
Referenced from: commits 19f3347