Announcement Announcement Module
No announcement yet.
Singletons or prototypes Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Singletons or prototypes

    Hi All,

    Just a very stupid simple question.....

    Are all steps and tasklets prototypes by default ?


  • #2
    no, they are just ordinary spring beans => singletons by default


    • #3
      I have asked similar question before and here is what I understood, wish it can help:

      Normally, tasklet is singleton by default. Therefore, if the tasklet's logic is stateless, it will run well.

      However, if you want to have a stateful, or want to be StepContextAware, you may make the scope of the tasklet being "step" (you should register the scope first, refer to the unit test), and put <aop:scoped-proxy/> in the tasklet's bean config in app context. It should make your tasklet instantiated for every new step.


      • #4
        For a little bit of background around the issue:

        When using steps, lots of very stateful resources are generally created. For example, if reading from the database you may open a cursor that stays open for the life of the batch job. This should be closed as soon as the step finishes, which is why we have step scope, to ensure resources are free. So, in theory, you could make your steps 'prototype scoped', but that doesn't necessarily guarantee that the resources created will be released without using something like step scope or closing the application context.

        As a side note, there's an issue to move the responsibilities of step scope to the step itself, but the overall effect is the same: