diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/scope/JobScope.java b/spring-batch-core/src/main/java/org/springframework/batch/core/scope/JobScope.java index 6bb8583cc8..c57513b435 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/scope/JobScope.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/scope/JobScope.java @@ -30,7 +30,7 @@ * this scope are <aop:scoped-proxy/> (no need to decorate the bean * definitions).
*
- * + *

* In addition, support is provided for late binding of references accessible from the * {@link JobContext} using #{..} placeholders. Using this feature, bean properties can be * pulled from the job or job execution context and the job parameters. E.g. @@ -44,7 +44,7 @@ * <property name="name" value="#{jobExecutionContext['input.stem']}.txt" /> * </bean> * - * + *

* The {@link JobContext} is referenced using standard bean property paths (as per * {@link BeanWrapper}). The examples above all show the use of the Map accessors provided * as a convenience for job attributes. @@ -56,9 +56,9 @@ */ public class JobScope extends BatchScopeSupport { - private static final String TARGET_NAME_PREFIX = "jobScopedTarget."; + private static final String TARGET_NAME_PREFIX = "scopedTarget."; - private Log logger = LogFactory.getLog(getClass()); + private final Log logger = LogFactory.getLog(getClass()); private final Object mutex = new Object(); diff --git a/spring-batch-test/src/test/java/org/springframework/batch/test/scopes/ScopeTestJobTest.java b/spring-batch-test/src/test/java/org/springframework/batch/test/scopes/ScopeTestJobTest.java new file mode 100644 index 0000000000..7a6b97ae23 --- /dev/null +++ b/spring-batch-test/src/test/java/org/springframework/batch/test/scopes/ScopeTestJobTest.java @@ -0,0 +1,50 @@ +/* + * Copyright 2009-2022 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.batch.test.scopes; + +import org.junit.jupiter.api.Test; +import org.springframework.batch.core.BatchStatus; +import org.springframework.batch.core.Job; +import org.springframework.batch.core.job.SimpleJob; +import org.springframework.batch.test.JobLauncherTestUtils; +import org.springframework.batch.test.JobRepositoryTestUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * This class will specifically test the capabilities of {@link JobRepositoryTestUtils} to + * test {@link SimpleJob}s. + * + * @author Dan Garrette + * @since 2.0 + */ +@SpringJUnitConfig( + locations = { "/simple-job-launcher-context.xml", "/jobs/scopeTests.xml", "/job-runner-context.xml" }) +class ScopeTestJobTest { + + @Autowired + private JobLauncherTestUtils jobLauncherTestUtils; + + @Test + void testJob(@Autowired Job job) throws Exception { + this.jobLauncherTestUtils.setJob(job); + assertEquals(BatchStatus.COMPLETED, jobLauncherTestUtils.launchJob().getStatus()); + + } + +} diff --git a/spring-batch-test/src/test/java/org/springframework/batch/test/scopes/ScopedConfiguration.java b/spring-batch-test/src/test/java/org/springframework/batch/test/scopes/ScopedConfiguration.java new file mode 100644 index 0000000000..0822a1dffe --- /dev/null +++ b/spring-batch-test/src/test/java/org/springframework/batch/test/scopes/ScopedConfiguration.java @@ -0,0 +1,49 @@ +package org.springframework.batch.test.scopes; + +import org.springframework.batch.core.configuration.annotation.JobScope; +import org.springframework.batch.core.configuration.annotation.StepScope; +import org.springframework.batch.core.step.tasklet.Tasklet; +import org.springframework.batch.repeat.RepeatStatus; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Scope; + +@Configuration +public class ScopedConfiguration { + + @Bean + @Scope("step") + ScopedInterface stepScope() { + return new ScopedImplementation(); + } + + @Bean + @Scope("job") + ScopedInterface jobScope() { + return new ScopedImplementation(); + } + + @Bean + @StepScope + ScopedInterface stepScopeAnnotaion() { + return new ScopedImplementation(); + } + + @Bean + @JobScope + ScopedInterface jobScopeAnnotation() { + return new ScopedImplementation(); + } + + @Bean + Tasklet scopeTestTasklet() { + return ((contribution, chunkContext) -> { + System.out.println("StepScope:" + stepScope().getInstanceNumber()); + System.out.println("StepScopeAnnotation:" + stepScopeAnnotaion().getInstanceNumber()); + System.out.println("JobScope:" + jobScope().getInstanceNumber()); + System.out.println("JobScopeAnnotation:" + jobScopeAnnotation().getInstanceNumber()); + return RepeatStatus.FINISHED; + }); + } + +} diff --git a/spring-batch-test/src/test/java/org/springframework/batch/test/scopes/ScopedImplementation.java b/spring-batch-test/src/test/java/org/springframework/batch/test/scopes/ScopedImplementation.java new file mode 100644 index 0000000000..962f986825 --- /dev/null +++ b/spring-batch-test/src/test/java/org/springframework/batch/test/scopes/ScopedImplementation.java @@ -0,0 +1,20 @@ +package org.springframework.batch.test.scopes; + +import java.util.concurrent.atomic.AtomicInteger; + +public class ScopedImplementation implements ScopedInterface { + + private final static AtomicInteger INSTANCE_COUNTER = new AtomicInteger(); + + private final int instance; + + public ScopedImplementation() { + this.instance = INSTANCE_COUNTER.incrementAndGet(); + } + + @Override + public int getInstanceNumber() { + return instance; + } + +} diff --git a/spring-batch-test/src/test/java/org/springframework/batch/test/scopes/ScopedInterface.java b/spring-batch-test/src/test/java/org/springframework/batch/test/scopes/ScopedInterface.java new file mode 100644 index 0000000000..b84666ca39 --- /dev/null +++ b/spring-batch-test/src/test/java/org/springframework/batch/test/scopes/ScopedInterface.java @@ -0,0 +1,7 @@ +package org.springframework.batch.test.scopes; + +public interface ScopedInterface { + + int getInstanceNumber(); + +} diff --git a/spring-batch-test/src/test/resources/jobs/scopeTests.xml b/spring-batch-test/src/test/resources/jobs/scopeTests.xml new file mode 100644 index 0000000000..7ba01e6326 --- /dev/null +++ b/spring-batch-test/src/test/resources/jobs/scopeTests.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + \ No newline at end of file