From 5fae33185fb92ac46f27d150404cc6be581b4749 Mon Sep 17 00:00:00 2001 From: Stephan Freund Date: Thu, 10 Jun 2021 00:12:41 +0200 Subject: [PATCH] Fix TARGET_NAME_PREFIX in StepScope Resolves #3936 --- .../batch/core/scope/StepScope.java | 4 +- .../StepScopeConfigurationTests.java | 40 +++++++++++++++++-- ...singNamespaceAutoregisterBeans-context.xml | 30 ++++++++++++++ 3 files changed, 68 insertions(+), 6 deletions(-) create mode 100644 spring-batch-core/src/test/resources/org/springframework/batch/core/configuration/annotation/StepScopeConfigurationTestsUsingNamespaceAutoregisterBeans-context.xml diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/scope/StepScope.java b/spring-batch-core/src/main/java/org/springframework/batch/core/scope/StepScope.java index fe81095ec..1454c5e8a 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/scope/StepScope.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/scope/StepScope.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2013 the original author or authors. + * Copyright 2006-2023 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. @@ -63,7 +63,7 @@ import org.springframework.beans.factory.config.Scope; */ public class StepScope extends BatchScopeSupport { - private static final String TARGET_NAME_PREFIX = "stepScopedTarget."; + private static final String TARGET_NAME_PREFIX = "scopedTarget."; private Log logger = LogFactory.getLog(getClass()); diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/annotation/StepScopeConfigurationTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/annotation/StepScopeConfigurationTests.java index ecce86bed..7cfb83036 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/annotation/StepScopeConfigurationTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/annotation/StepScopeConfigurationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2022 the original author or authors. + * Copyright 2006-2023 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. @@ -16,9 +16,12 @@ package org.springframework.batch.core.configuration.annotation; +import java.util.concurrent.Callable; + import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; + import org.springframework.batch.core.StepContribution; import org.springframework.batch.core.StepExecution; import org.springframework.batch.core.scope.context.ChunkContext; @@ -38,8 +41,6 @@ import org.springframework.context.annotation.ScopedProxyMode; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.lang.Nullable; -import java.util.concurrent.Callable; - import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -101,6 +102,19 @@ public class StepScopeConfigurationTests { assertEquals("STEP", value.call()); } + /** + * @see org.springframework.batch.core.configuration.xml.CoreNamespaceUtils#autoregisterBeansForNamespace + */ + @Test + public void testStepScopeUsingNamespaceAutoregisterBeans() throws Exception { + init(StepScopeConfigurationTestsUsingNamespaceAutoregisterBeans.class); + + ISimpleHolder value = (ISimpleHolder) context.getBean("xmlValue"); + assertEquals("STEP", value.call()); + value = (ISimpleHolder) context.getBean("javaValue"); + assertEquals("STEP", value.call()); + } + @Test void testStepScopeWithProxyTargetClassInjected() throws Exception { init(StepScopeConfigurationInjectingProxy.class); @@ -198,7 +212,13 @@ public class StepScopeConfigurationTests { } - public static class SimpleHolder { + public static interface ISimpleHolder { + + String call() throws Exception; + + } + + public static class SimpleHolder implements ISimpleHolder { private final String value; @@ -243,6 +263,18 @@ public class StepScopeConfigurationTests { } + @Configuration + @ImportResource("org/springframework/batch/core/configuration/annotation/StepScopeConfigurationTestsUsingNamespaceAutoregisterBeans-context.xml") + public static class StepScopeConfigurationTestsUsingNamespaceAutoregisterBeans { + + @Bean + @StepScope + protected SimpleHolder javaValue(@Value("#{stepExecution.stepName}") final String value) { + return new SimpleHolder(value); + } + + } + @Configuration @EnableBatchProcessing public static class StepScopeConfigurationInjectingProxy { diff --git a/spring-batch-core/src/test/resources/org/springframework/batch/core/configuration/annotation/StepScopeConfigurationTestsUsingNamespaceAutoregisterBeans-context.xml b/spring-batch-core/src/test/resources/org/springframework/batch/core/configuration/annotation/StepScopeConfigurationTestsUsingNamespaceAutoregisterBeans-context.xml new file mode 100644 index 000000000..00b9a7e65 --- /dev/null +++ b/spring-batch-core/src/test/resources/org/springframework/batch/core/configuration/annotation/StepScopeConfigurationTestsUsingNamespaceAutoregisterBeans-context.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + + + + + + + +