Click here to Skip to main content
15,903,203 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to store some state, I receive in the processor, for each of the StepExecutions separately. For this purpose I created a step-scoped bean, which I try to inject:

Java
@Slf4j
@Configuration
@EnableScheduling
@EnableBatchProcessing
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class SomeConfiguration
{
// here go the job definition with one step, which has a reader, writer and processor
    @StepScope
    @Bean
    public SomeProcessor processingStep(StateService s)
    {
        var processor = new SomeProcessor();
        processor.setStService(s);
        return processor;
    }

    // StateService is an empty class
    @Scope(value = "step", proxyMode = ScopedProxyMode.NO)
    @Bean
    public StateService stateService()
    {
        return new StateService();
    }
}


Whatever I do, I get either NoSuchBeanDefinitionException or ClassCastException, since Spring Batch doesn't get along with its proxies. How to use @StepScope in Spring Batch? There is literally no documentation about how these @StepScope and @JobScope work, just an incomplete example here (only a declaration example, no clues of how to use step-scoped bean).

What I have tried:

Calling stateService bean directly:
processor.setStService(stateService());

Changing ScopedProxyMode to all possible values;

Injecting stateService directly into processor:
Java
public class SomeProcessor implements ItemProcessor<EFrom, ETo>
{
    // ...
    @Autowired
    private StateService stService;
    // ...
}


Removing @EnableBatchProcessing annotation from configuration file.
Posted
Updated 12-Oct-22 7:37am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900