diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/BatchBeanDefinitionRegistryPostProcessor.java b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/BatchBeanDefinitionRegistryPostProcessor.java new file mode 100644 index 0000000000..871ad691fb --- /dev/null +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/BatchBeanDefinitionRegistryPostProcessor.java @@ -0,0 +1,46 @@ +/* + * Copyright 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. + * 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.support; + +import org.springframework.beans.factory.config.BeanDefinition; +import org.springframework.beans.factory.support.BeanDefinitionBuilder; +import org.springframework.beans.factory.support.BeanDefinitionRegistry; +import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor; + +public class BatchBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor { + + private static final String JOB_REGISTRY = "jobRegistry"; + + private static final String BEAN_POST_PROCESSOR = "jobRegistryBeanPostProcessor"; + + @Override + public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) { + if (!registry.containsBeanDefinition(JOB_REGISTRY)) { + var beanDefinition = BeanDefinitionBuilder.genericBeanDefinition(MapJobRegistry.class) + .setRole(BeanDefinition.ROLE_INFRASTRUCTURE) + .getBeanDefinition(); + registry.registerBeanDefinition(JOB_REGISTRY, beanDefinition); + } + + if (!registry.containsBeanDefinition(BEAN_POST_PROCESSOR)) { + var beanDefinition = BeanDefinitionBuilder.genericBeanDefinition(JobRegistryBeanPostProcessor.class) + .addPropertyReference(JOB_REGISTRY, JOB_REGISTRY) + .getBeanDefinition(); + registry.registerBeanDefinition(BEAN_POST_PROCESSOR, beanDefinition); + } + } + +} diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/DefaultBatchConfiguration.java b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/DefaultBatchConfiguration.java index eee6738096..92e5257771 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/DefaultBatchConfiguration.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/support/DefaultBatchConfiguration.java @@ -112,13 +112,18 @@ * @since 5.0 */ @Configuration(proxyBeanMethods = false) -@Import(ScopeConfiguration.class) +@Import({ ScopeConfiguration.class, BatchBeanDefinitionRegistryPostProcessor.class }) public class DefaultBatchConfiguration implements ApplicationContextAware { @Autowired protected ApplicationContext applicationContext; - private final JobRegistry jobRegistry = new MapJobRegistry(); + @Autowired + protected JobRegistry jobRegistry; + + @Autowired + @Deprecated(forRemoval = true) + private JobRegistryBeanPostProcessor jobRegistryBeanPostProcessor; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { @@ -187,9 +192,17 @@ public JobExplorer jobExplorer() throws BatchConfigurationException { } } - @Bean + /** + * Defines a {@link JobRegistry} bean. + * @return a {@link JobRegistry} bean + * @throws BatchConfigurationException if unable to register the bean + * @since 5.0 + * @deprecated in favor of bean injection, and declaring a custom bean with name + * {@code jobRegistry} + */ + @Deprecated(forRemoval = true) public JobRegistry jobRegistry() throws BatchConfigurationException { - return this.jobRegistry; // FIXME returning a new instance here does not work + return this.jobRegistry; } @Bean @@ -214,18 +227,12 @@ public JobOperator jobOperator() throws BatchConfigurationException { * @return a {@link JobRegistryBeanPostProcessor} bean * @throws BatchConfigurationException if unable to register the bean * @since 5.1 + * @deprecated in favor of bean injection, and declaring a custom bean with name + * {@code jobRegistryBeanPostProcessor} */ - @Bean + @Deprecated(forRemoval = true) public JobRegistryBeanPostProcessor jobRegistryBeanPostProcessor() throws BatchConfigurationException { - JobRegistryBeanPostProcessor jobRegistryBeanPostProcessor = new JobRegistryBeanPostProcessor(); - jobRegistryBeanPostProcessor.setJobRegistry(jobRegistry()); - try { - jobRegistryBeanPostProcessor.afterPropertiesSet(); - return jobRegistryBeanPostProcessor; - } - catch (Exception e) { - throw new BatchConfigurationException("Unable to configure the default job registry BeanPostProcessor", e); - } + return this.jobRegistryBeanPostProcessor; } /* diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/support/DefaultBatchConfigurationTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/support/DefaultBatchConfigurationTests.java index 6c54df5a90..7de654297a 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/support/DefaultBatchConfigurationTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/support/DefaultBatchConfigurationTests.java @@ -160,7 +160,7 @@ public JobRepository jobRepository() { } @Bean - public JobRegistryBeanPostProcessor jobRegistryBeanPostProcessor(JobRegistry jobRegistry) { + public static JobRegistryBeanPostProcessor jobRegistryBeanPostProcessor(JobRegistry jobRegistry) { JobRegistryBeanPostProcessor postProcessor = new JobRegistryBeanPostProcessor(); postProcessor.setJobRegistry(jobRegistry); return postProcessor;