Skip to content

Commit 5fae331

Browse files
farnettofmbenhassine
authored andcommitted
Fix TARGET_NAME_PREFIX in StepScope
Resolves #3936
1 parent ab9dbc3 commit 5fae331

File tree

3 files changed

+68
-6
lines changed

3 files changed

+68
-6
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2013 the original author or authors.
2+
* Copyright 2006-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -63,7 +63,7 @@
6363
*/
6464
public class StepScope extends BatchScopeSupport {
6565

66-
private static final String TARGET_NAME_PREFIX = "stepScopedTarget.";
66+
private static final String TARGET_NAME_PREFIX = "scopedTarget.";
6767

6868
private Log logger = LogFactory.getLog(getClass());
6969

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

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2022 the original author or authors.
2+
* Copyright 2006-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,9 +16,12 @@
1616

1717
package org.springframework.batch.core.configuration.annotation;
1818

19+
import java.util.concurrent.Callable;
20+
1921
import org.junit.jupiter.api.AfterEach;
2022
import org.junit.jupiter.api.BeforeEach;
2123
import org.junit.jupiter.api.Test;
24+
2225
import org.springframework.batch.core.StepContribution;
2326
import org.springframework.batch.core.StepExecution;
2427
import org.springframework.batch.core.scope.context.ChunkContext;
@@ -38,8 +41,6 @@
3841
import org.springframework.context.support.ClassPathXmlApplicationContext;
3942
import org.springframework.lang.Nullable;
4043

41-
import java.util.concurrent.Callable;
42-
4344
import static org.junit.jupiter.api.Assertions.assertEquals;
4445
import static org.junit.jupiter.api.Assertions.assertThrows;
4546
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -101,6 +102,19 @@ void testStepScopeXmlImportUsingNamespace() throws Exception {
101102
assertEquals("STEP", value.call());
102103
}
103104

105+
/**
106+
* @see org.springframework.batch.core.configuration.xml.CoreNamespaceUtils#autoregisterBeansForNamespace
107+
*/
108+
@Test
109+
public void testStepScopeUsingNamespaceAutoregisterBeans() throws Exception {
110+
init(StepScopeConfigurationTestsUsingNamespaceAutoregisterBeans.class);
111+
112+
ISimpleHolder value = (ISimpleHolder) context.getBean("xmlValue");
113+
assertEquals("STEP", value.call());
114+
value = (ISimpleHolder) context.getBean("javaValue");
115+
assertEquals("STEP", value.call());
116+
}
117+
104118
@Test
105119
void testStepScopeWithProxyTargetClassInjected() throws Exception {
106120
init(StepScopeConfigurationInjectingProxy.class);
@@ -198,7 +212,13 @@ public String call() throws Exception {
198212

199213
}
200214

201-
public static class SimpleHolder {
215+
public static interface ISimpleHolder {
216+
217+
String call() throws Exception;
218+
219+
}
220+
221+
public static class SimpleHolder implements ISimpleHolder {
202222

203223
private final String value;
204224

@@ -243,6 +263,18 @@ protected SimpleHolder javaValue(@Value("#{stepExecution.stepName}") final Strin
243263

244264
}
245265

266+
@Configuration
267+
@ImportResource("org/springframework/batch/core/configuration/annotation/StepScopeConfigurationTestsUsingNamespaceAutoregisterBeans-context.xml")
268+
public static class StepScopeConfigurationTestsUsingNamespaceAutoregisterBeans {
269+
270+
@Bean
271+
@StepScope
272+
protected SimpleHolder javaValue(@Value("#{stepExecution.stepName}") final String value) {
273+
return new SimpleHolder(value);
274+
}
275+
276+
}
277+
246278
@Configuration
247279
@EnableBatchProcessing
248280
public static class StepScopeConfigurationInjectingProxy {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns:batch="http://www.springframework.org/schema/batch"
5+
xsi:schemaLocation="
6+
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-3.1.xsd
7+
http://www.springframework.org/schema/batch https://www.springframework.org/schema/batch/spring-batch.xsd">
8+
9+
<bean id="tasklet"
10+
class="org.springframework.batch.core.configuration.annotation.StepScopeConfigurationTests$TaskletSupport" />
11+
12+
<batch:job id="job">
13+
<batch:step id="step1">
14+
<batch:tasklet ref="tasklet" />
15+
</batch:step>
16+
</batch:job>
17+
18+
<bean id="xmlValue"
19+
class="org.springframework.batch.core.configuration.annotation.StepScopeConfigurationTests.SimpleHolder"
20+
scope="step">
21+
<constructor-arg value="#{stepExecution.stepName}" />
22+
</bean>
23+
24+
<batch:job-repository id="jobRepository" />
25+
26+
<bean id="transactionManager"
27+
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
28+
<property name="dataSource" ref="dataSource" />
29+
</bean>
30+
</beans>

0 commit comments

Comments
 (0)