Spring batch scope issue while using spring boot

北战南征 提交于 2019-11-27 20:16:41

This may be a bug (we're still investigating), however we do have a work around. The cause of this is that when using @EnableBatchProcessing the StepScope that is automatically configured assumes java config and therefore does not proxy the step scoped beans, causing them to be created too soon. The work around is to manually configure a StepScope in your XML configuration with the following configuration:

<bean id="stepScope" class="org.springframework.batch.core.scope.StepScope">
    <property name="autoProxy" value="true"/>
</bean>

Michael's comment is working for me, I am also providing JavaConfig copy-paste alternative for lazy people like me :)

@Bean
public StepScope stepScope() {
    final StepScope stepScope = new StepScope();
    stepScope.setAutoProxy(true);
    return stepScope;
}

Seeing as you are using @RunWith(SpringRunner.class), declaring @TestExecutionListeners({..., StepScopeTestExecutionListener.class}) above your class will setup the scopes for you.

Same with @TestExecutionListeners({..., JobScopeTestExecutionListener.class}) for jobScope.

Don't forget to configure

spring.main.allow-bean-definition-overriding=true
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!