Skip to content

Commit 799f634

Browse files
committed
BATCH-2552 BeanDefinitionStoreException with StepScope
Fixes BATCH-2552 Fixes spring-projects#1050
1 parent 277963b commit 799f634

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

spring-batch-core/src/main/java/org/springframework/batch/core/scope/BatchScopeSupport.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.springframework.beans.factory.config.Scope;
2727
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils;
2828
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
29+
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
2930
import org.springframework.core.Ordered;
3031
import org.springframework.util.Assert;
3132
import org.springframework.util.StringValueResolver;
@@ -127,6 +128,9 @@ public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
127128
scopifier.visitBeanDefinition(definition);
128129

129130
if (scoped && !definition.isAbstract()) {
131+
if (beanFactory instanceof DefaultListableBeanFactory) {
132+
((DefaultListableBeanFactory)beanFactory).removeBeanDefinition(beanName);
133+
}
130134
createScopedProxy(beanName, definition, registry, proxyTargetClass);
131135
}
132136
}
@@ -139,7 +143,7 @@ public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
139143
* after the {@link StepContext} is available. Amounts to adding
140144
* <aop-auto-proxy/> to a step scoped bean.
141145
*
142-
* @param beanName the bean name to replace
146+
* @param targetProxyBeanName the bean name to replace
143147
* @param definition the bean definition to replace
144148
* @param registry the enclosing {@link BeanDefinitionRegistry}
145149
* @param proxyTargetClass true if we need to force use of dynamic
@@ -148,15 +152,14 @@ public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
148152
* target. Caller should register it if needed to be visible at top level in
149153
* bean factory.
150154
*/
151-
protected static BeanDefinitionHolder createScopedProxy(String beanName, BeanDefinition definition,
155+
protected static BeanDefinitionHolder createScopedProxy(String targetProxyBeanName, BeanDefinition definition,
152156
BeanDefinitionRegistry registry, boolean proxyTargetClass) {
153-
154157
BeanDefinitionHolder proxyHolder;
155158

156-
proxyHolder = ScopedProxyUtils.createScopedProxy(new BeanDefinitionHolder(definition, beanName), registry,
159+
proxyHolder = ScopedProxyUtils.createScopedProxy(new BeanDefinitionHolder(definition, targetProxyBeanName), registry,
157160
proxyTargetClass);
158161

159-
registry.registerBeanDefinition(beanName, proxyHolder.getBeanDefinition());
162+
registry.registerBeanDefinition(targetProxyBeanName, proxyHolder.getBeanDefinition());
160163

161164
return proxyHolder;
162165

spring-batch-core/src/test/java/org/springframework/batch/core/configuration/annotation/StepScopeConfigurationTests.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,13 @@ public void testIntentionallyBlowUpOnMissingContextWithInterface() throws Except
148148
Callable<String> value = context.getBean(Callable.class);
149149
assertEquals("STEP", value.call());
150150
}
151+
152+
@Test
153+
public void testStepScopeAnnotationWithoutEnableBatchProcessing() throws Exception {
154+
init(StepScopeConfigurationWithoutEnableBatchProcessing.class);
155+
Callable value = context.getBean(Callable.class);
156+
assertEquals("STEP", value.call());
157+
}
151158

152159
public void init(Class<?>... config) throws Exception {
153160
Class<?>[] configs = new Class<?>[config.length + 1];
@@ -296,4 +303,19 @@ public RepeatStatus execute(StepContribution contribution, ChunkContext chunkCon
296303
return RepeatStatus.FINISHED;
297304
}
298305
}
306+
307+
@Configuration
308+
public static class StepScopeConfigurationWithoutEnableBatchProcessing {
309+
@Bean
310+
public static org.springframework.batch.core.scope.StepScope stepScope() {
311+
return new org.springframework.batch.core.scope.StepScope();
312+
}
313+
314+
@Bean
315+
@StepScope
316+
protected Callable<String> value(@Value("#{stepExecution.stepName}")
317+
final String value) {
318+
return new SimpleCallable(value);
319+
}
320+
}
299321
}

0 commit comments

Comments
 (0)