0
# Auto-Configuration
1
2
Spring Boot Batch Starter's auto-configuration automatically sets up Spring Batch infrastructure when the necessary dependencies are present on the classpath.
3
4
## Capabilities
5
6
### BatchAutoConfiguration
7
8
Main auto-configuration class that provides comprehensive Spring Batch setup with minimal configuration required.
9
10
```java { .api }
11
/**
12
* Auto-configuration for Spring Batch. Automatically configures batch processing
13
* infrastructure when JobLauncher, DataSource, and DatabasePopulator are available.
14
*/
15
@AutoConfiguration(after = { HibernateJpaAutoConfiguration.class, TransactionAutoConfiguration.class })
16
@ConditionalOnClass({ JobLauncher.class, DataSource.class, DatabasePopulator.class })
17
@ConditionalOnBean({ DataSource.class, PlatformTransactionManager.class })
18
@ConditionalOnMissingBean(value = DefaultBatchConfiguration.class, annotation = EnableBatchProcessing.class)
19
@EnableConfigurationProperties(BatchProperties.class)
20
public class BatchAutoConfiguration {
21
22
/**
23
* Creates JobLauncherApplicationRunner for automatic job execution on startup
24
*/
25
@Bean
26
@ConditionalOnMissingBean
27
@ConditionalOnBooleanProperty(name = "spring.batch.job.enabled", matchIfMissing = true)
28
public JobLauncherApplicationRunner jobLauncherApplicationRunner(
29
JobLauncher jobLauncher,
30
JobExplorer jobExplorer,
31
JobRepository jobRepository,
32
BatchProperties properties);
33
34
/**
35
* Creates exit code generator for mapping job execution results to application exit codes
36
*/
37
@Bean
38
@ConditionalOnMissingBean(ExitCodeGenerator.class)
39
public JobExecutionExitCodeGenerator jobExecutionExitCodeGenerator();
40
}
41
```
42
43
**Usage Examples:**
44
45
```java
46
// Auto-configuration activates when these components are present
47
@SpringBootApplication
48
public class BatchApplication {
49
// No explicit batch configuration needed - auto-configured automatically
50
51
@Bean
52
public Job myJob(JobRepository jobRepository, Step myStep) {
53
return new JobBuilder("myJob", jobRepository)
54
.start(myStep)
55
.build();
56
}
57
}
58
59
// Disable automatic job execution
60
# application.properties
61
spring.batch.job.enabled=false
62
```
63
64
### SpringBootBatchConfiguration
65
66
Internal configuration class that extends Spring Batch's `DefaultBatchConfiguration` with Spring Boot specific customizations.
67
68
```java { .api }
69
/**
70
* Spring Boot specific batch configuration that customizes DefaultBatchConfiguration
71
* with application properties and Spring Boot conventions
72
*/
73
@Configuration(proxyBeanMethods = false)
74
static class SpringBootBatchConfiguration extends DefaultBatchConfiguration {
75
76
/**
77
* Constructor with dependency injection for all batch infrastructure components
78
*/
79
SpringBootBatchConfiguration(DataSource dataSource,
80
@BatchDataSource ObjectProvider<DataSource> batchDataSource,
81
PlatformTransactionManager transactionManager,
82
@BatchTransactionManager ObjectProvider<PlatformTransactionManager> batchTransactionManager,
83
@BatchTaskExecutor ObjectProvider<TaskExecutor> batchTaskExecutor,
84
BatchProperties properties,
85
ObjectProvider<BatchConversionServiceCustomizer> batchConversionServiceCustomizers,
86
ObjectProvider<ExecutionContextSerializer> executionContextSerializer,
87
ObjectProvider<JobParametersConverter> jobParametersConverter);
88
89
/**
90
* Provides the DataSource for batch metadata storage
91
*/
92
protected DataSource getDataSource();
93
94
/**
95
* Provides the transaction manager for batch operations
96
*/
97
protected PlatformTransactionManager getTransactionManager();
98
99
/**
100
* Provides custom table prefix for batch metadata tables
101
*/
102
protected String getTablePrefix();
103
104
/**
105
* Controls transaction state validation behavior
106
*/
107
protected boolean getValidateTransactionState();
108
109
/**
110
* Sets transaction isolation level for job metadata creation
111
*/
112
protected Isolation getIsolationLevelForCreate();
113
114
/**
115
* Provides customized conversion service for batch operations
116
*/
117
protected ConfigurableConversionService getConversionService();
118
119
/**
120
* Provides custom execution context serializer if configured
121
*/
122
protected ExecutionContextSerializer getExecutionContextSerializer();
123
124
/**
125
* Provides custom job parameters converter if configured
126
*/
127
protected JobParametersConverter getJobParametersConverter();
128
129
/**
130
* Provides custom task executor for batch operations if configured
131
*/
132
protected TaskExecutor getTaskExecutor();
133
}
134
```
135
136
### Database Initialization Configuration
137
138
Automatic database schema initialization for Spring Batch metadata tables.
139
140
```java { .api }
141
/**
142
* Configuration for automatic database schema initialization
143
*/
144
@Configuration(proxyBeanMethods = false)
145
@Conditional(OnBatchDatasourceInitializationCondition.class)
146
static class DataSourceInitializerConfiguration {
147
148
/**
149
* Creates database initializer for batch schema setup
150
*/
151
@Bean
152
@ConditionalOnMissingBean
153
public BatchDataSourceScriptDatabaseInitializer batchDataSourceInitializer(
154
DataSource dataSource,
155
@BatchDataSource ObjectProvider<DataSource> batchDataSource,
156
BatchProperties properties);
157
}
158
159
/**
160
* Condition that determines when batch database initialization should occur
161
*/
162
static class OnBatchDatasourceInitializationCondition extends OnDatabaseInitializationCondition {
163
164
public OnBatchDatasourceInitializationCondition();
165
}
166
```
167
168
## Activation Conditions
169
170
Auto-configuration activates when:
171
172
1. **Required Classes**: `JobLauncher`, `DataSource`, and `DatabasePopulator` are on classpath
173
2. **Required Beans**: `DataSource` and `PlatformTransactionManager` beans are available
174
3. **Exclusion Conditions**: No `DefaultBatchConfiguration` bean or `@EnableBatchProcessing` annotation present
175
4. **Property Conditions**: `spring.batch.job.enabled` is not explicitly set to `false`
176
177
## Customization Points
178
179
- Use `@BatchDataSource` to specify alternative data source for batch metadata
180
- Use `@BatchTransactionManager` to specify alternative transaction manager
181
- Use `@BatchTaskExecutor` to specify alternative task executor
182
- Implement `BatchConversionServiceCustomizer` to customize type conversion
183
- Configure `ExecutionContextSerializer` bean for custom serialization
184
- Configure `JobParametersConverter` bean for custom parameter conversion
185
186
## Integration Dependencies
187
188
Auto-configuration integrates with:
189
190
- **Spring Boot Starter JDBC**: Provides DataSource and database connectivity
191
- **Spring Batch Core**: Provides core batch processing framework
192
- **Spring Boot Actuator**: Enables monitoring and management endpoints
193
- **Micrometer**: Provides observability and metrics collection