Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any valid reason for declaring a Quartz Job as a spring bean?

By looking at some examples of running quartz jobs in a spring boot app, I see many of them are actually declaring the Job with the @Component annotation :

  • Baeldung example
  • Dzone example
  • Medium example

It seems to be completly useless as the job factory will create a new instance every time the job is triggered.

I can see in my app that the method SpringBeanJobFactory.createJobInstance is called each time the job is executed. I removed the @Component annotation and it works perfectly, so is there any advantage of declaring a Job as a spring bean ?

like image 278
Olivier Boissé Avatar asked Oct 20 '25 17:10

Olivier Boissé


1 Answers

In some cases you need to inject spring managed components into your job, for example some service or repository (to get some data to process by job). In such case, you wanna create a job as a component and allows spring to inject required beans into it. In such case your job's superclass can extend QuartzJobBean.

like image 148
AdrianG Avatar answered Oct 23 '25 08:10

AdrianG



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!