Skip to content

Commit 48e437a

Browse files
committed
Revisit the configuration code of EnableBatchProcessing
Before this commit, the configuration of infrastructure beans was confusing and not straightforward to customize. This commit changes the way Batch infrastructure beans are configured. The most important changes are: * EnableBatchProcessing now provides new attributes to configure properties of infrastructure beans * Bean registration is now done programmatically with a BeanDefinitionRegistrar instead of importing a class with statically annotated bean definition methods * Bean are now resolved from the application context directly instead of being resolved from a BatchConfigurer * Both a data source and a transaction manager are now required to be defined in the application context * A new configuration class called DefaultBatchConfiguration with default infrastructure bean definitions is now provided and can be extended to customize the default configuration Resolves #3942
1 parent f39f070 commit 48e437a

25 files changed

+1131
-969
lines changed

spring-batch-core/src/main/java/org/springframework/batch/core/configuration/BatchConfigurationException.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,21 @@ public BatchConfigurationException(Throwable t) {
3636
super(t);
3737
}
3838

39+
/**
40+
* Create an exception with the given message.
41+
* @param message the error message
42+
*/
43+
public BatchConfigurationException(String message) {
44+
super(message);
45+
}
46+
47+
/**
48+
* Create an exception with the given message and {@link Throwable}.
49+
* @param message the error message
50+
* @param cause an exception to be wrapped
51+
*/
52+
public BatchConfigurationException(String message, Throwable cause) {
53+
super(message, cause);
54+
}
55+
3956
}

spring-batch-core/src/main/java/org/springframework/batch/core/configuration/annotation/AbstractBatchConfiguration.java

Lines changed: 0 additions & 145 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.batch.core.configuration.annotation;
17+
18+
import java.util.Iterator;
19+
20+
import org.springframework.batch.core.configuration.JobRegistry;
21+
import org.springframework.batch.core.configuration.support.ApplicationContextFactory;
22+
import org.springframework.batch.core.configuration.support.AutomaticJobRegistrar;
23+
import org.springframework.batch.core.configuration.support.DefaultJobLoader;
24+
import org.springframework.beans.BeansException;
25+
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
26+
import org.springframework.beans.factory.config.BeanPostProcessor;
27+
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
28+
29+
/**
30+
* Post processor that configures the {@link AutomaticJobRegistrar} registered by
31+
* {@link BatchRegistrar} with required properties.
32+
*
33+
* @author Mahmoud Ben Hassine
34+
* @since 5.0
35+
*/
36+
class AutomaticJobRegistrarBeanPostProcessor implements BeanFactoryPostProcessor, BeanPostProcessor {
37+
38+
private ConfigurableListableBeanFactory beanFactory;
39+
40+
@Override
41+
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
42+
this.beanFactory = beanFactory;
43+
}
44+
45+
@Override
46+
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
47+
if (bean instanceof AutomaticJobRegistrar) {
48+
AutomaticJobRegistrar automaticJobRegistrar = (AutomaticJobRegistrar) bean;
49+
automaticJobRegistrar.setJobLoader(new DefaultJobLoader(this.beanFactory.getBean(JobRegistry.class)));
50+
for (ApplicationContextFactory factory : this.beanFactory.getBeansOfType(ApplicationContextFactory.class)
51+
.values()) {
52+
automaticJobRegistrar.addApplicationContextFactory(factory);
53+
}
54+
return automaticJobRegistrar;
55+
}
56+
return bean;
57+
}
58+
59+
}

spring-batch-core/src/main/java/org/springframework/batch/core/configuration/annotation/BatchConfigurationSelector.java

Lines changed: 0 additions & 53 deletions
This file was deleted.

spring-batch-core/src/main/java/org/springframework/batch/core/configuration/annotation/BatchConfigurer.java

Lines changed: 0 additions & 51 deletions
This file was deleted.

0 commit comments

Comments
 (0)