Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
spring-issuemaster opened this issue Oct 2, 2018 · 6 comments
Assignees
Milestone

Comments

@spring-issuemaster
Copy link
Collaborator

@spring-issuemaster spring-issuemaster commented Oct 2, 2018

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:

  • #19337 Ability to suppress "rejectedValue" in error responses

Referenced from: commits 19f3347

@spring-issuemaster
Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Oct 3, 2018

Stéphane Nicoll commented

Juergen Hoeller Spring Boot has a AutowireCapableBeanJobFactory that looks it does some part of what is requested here.

If we intend to retrofit that feature in Framework we should probably sync. We just got a PR with a potential bug in it

@spring-issuemaster
Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Oct 3, 2018

m-kay commented

I'm actually using the AutowireCapableBeanJobFactory however its parent is using ReflectionUtils#accessibleConstructor to instantiate the object and this prevents me from injecting arguments in the constructor.

@spring-issuemaster
Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Oct 3, 2018

Stéphane Nicoll commented

m-kay I got that, that's why I am mentioning here that part if not all this feature could move to the framework.

@spring-issuemaster
Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Oct 3, 2018

Juergen Hoeller commented

Indeed, I'm wondering whether we should simply include a fully-featured version of this in our core Quartz support package. If we do this for 5.1.1 still, Boot 2.1 RC1 may adapt to it right away.

@spring-issuemaster
Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Oct 4, 2018

Marten Deinum commented

I guess #19040 is related. 

@spring-issuemaster
Copy link
Collaborator Author

@spring-issuemaster spring-issuemaster commented Oct 9, 2018

Juergen Hoeller commented

This is addressed through full autowiring support in SpringBeanJobFactory now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.