From b38fd6ee19840921f3efd5712aafbcc6e6ed482c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henning=20P=C3=B6ttker?= Date: Fri, 2 Jul 2021 18:20:53 +0200 Subject: [PATCH] Make ScopeConfiguration publicly accessible When using a hierarchy of Spring contexts, custom scopes are not visible in child contexts. With this change, it is now possible for users to import the configuration class into child contexts and use job and step scope within them. --- .../AbstractBatchConfiguration.java | 36 +------------ .../annotation/ScopeConfiguration.java | 52 +++++++++++++++++++ 2 files changed, 53 insertions(+), 35 deletions(-) create mode 100644 spring-batch-core/src/main/java/org/springframework/batch/core/configuration/annotation/ScopeConfiguration.java diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/annotation/AbstractBatchConfiguration.java b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/annotation/AbstractBatchConfiguration.java index 4bfd2f19b8..f5a87595b0 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/annotation/AbstractBatchConfiguration.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/annotation/AbstractBatchConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2018 the original author or authors. + * Copyright 2012-2021 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. @@ -24,8 +24,6 @@ import org.springframework.batch.core.explore.JobExplorer; import org.springframework.batch.core.launch.JobLauncher; import org.springframework.batch.core.repository.JobRepository; -import org.springframework.batch.core.scope.JobScope; -import org.springframework.batch.core.scope.StepScope; import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; @@ -130,35 +128,3 @@ protected BatchConfigurer getConfigurer(Collection configurers) } } - -/** - * Extract job/step scope configuration into a separate unit. - * - * @author Dave Syer - * - */ -@Configuration(proxyBeanMethods = false) -class ScopeConfiguration { - - private static StepScope stepScope; - - private static JobScope jobScope; - - static { - jobScope = new JobScope(); - jobScope.setAutoProxy(false); - - stepScope = new StepScope(); - stepScope.setAutoProxy(false); - } - - @Bean - public static StepScope stepScope() { - return stepScope; - } - - @Bean - public static JobScope jobScope() { - return jobScope; - } -} diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/annotation/ScopeConfiguration.java b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/annotation/ScopeConfiguration.java new file mode 100644 index 0000000000..38c8f0f5d6 --- /dev/null +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/annotation/ScopeConfiguration.java @@ -0,0 +1,52 @@ +/* + * Copyright 2021 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.core.configuration.annotation; + +import org.springframework.batch.core.scope.JobScope; +import org.springframework.batch.core.scope.StepScope; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * {@code Configuration} class that provides {@link StepScope} and {@link JobScope}. + * + * @author Dave Syer + */ +@Configuration(proxyBeanMethods = false) +public class ScopeConfiguration { + + private static StepScope stepScope; + + private static JobScope jobScope; + + static { + jobScope = new JobScope(); + jobScope.setAutoProxy(false); + + stepScope = new StepScope(); + stepScope.setAutoProxy(false); + } + + @Bean + public static StepScope stepScope() { + return stepScope; + } + + @Bean + public static JobScope jobScope() { + return jobScope; + } +}