Olivier Boissé
Olivier Boissé

Reputation: 18153

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 :

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 ?

Upvotes: 4

Views: 1027

Answers (1)

AdrianG
AdrianG

Reputation: 41

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.

Upvotes: 4

Related Questions