Home > Mobile >  Are beans created every time when spring batch application runs on scheduler?
Are beans created every time when spring batch application runs on scheduler?

Time:09-07

I have one spring batch application which runs on scheduler. First time when application boots up, the beans of reader, processor and writer are created. My application is scheduled to run on specific scheduled. My doubt is whether these beans of reader, writer and processor gets created everytime newly when the job runs on schedule or older beans created at boot up time are getting used ?

CodePudding user response:

Spring Batch beans are singletons or they can by annotated with @JobScope meaning there is only one instance for each job or @StepScope where there will be an instance per step.

In your case you need to specify @JobScope or @StepScope if you want the beans to be reinstantiated for each scheduled job.

https://docs.spring.io/spring-batch/docs/current/reference/html/index-single.html#job-scope

https://docs.spring.io/spring-batch/docs/current/reference/html/index-single.html#step-scope

CodePudding user response:

Beans created with the annotation @Bean have a singleton scope by default. So your beans will not be created every time. If you want a fresh instance every time they are used, change the scope of the bean to request. See https://www.baeldung.com/spring-bean-scopes.

  • Related