Skip to content

Quartz job bean can't have constructor with injected parameters [SPR-17323] #21857

Closed
@spring-projects-issues

Description

@spring-projects-issues

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:

Referenced from: commits 19f3347

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions