Skip to content

Commit 24d03b2

Browse files
committed
Add possibility to configure a custom JobParametersConverter in BatchAutoConfiguration
Fix GH-44793 Signed-off-by: Yanming Zhou <zhouyanming@gmail.com>
1 parent 1646fa6 commit 24d03b2

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfiguration.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
2424
import org.springframework.batch.core.configuration.support.DefaultBatchConfiguration;
25+
import org.springframework.batch.core.converter.JobParametersConverter;
2526
import org.springframework.batch.core.explore.JobExplorer;
2627
import org.springframework.batch.core.launch.JobLauncher;
2728
import org.springframework.batch.core.repository.ExecutionContextSerializer;
@@ -66,6 +67,7 @@
6667
* @author Mahmoud Ben Hassine
6768
* @author Lars Uffmann
6869
* @author Lasse Wulff
70+
* @author Yanming Zhou
6971
* @since 1.0.0
7072
*/
7173
@AutoConfiguration(after = { HibernateJpaAutoConfiguration.class, TransactionAutoConfiguration.class })
@@ -110,18 +112,22 @@ static class SpringBootBatchConfiguration extends DefaultBatchConfiguration {
110112

111113
private final ExecutionContextSerializer executionContextSerializer;
112114

115+
private final JobParametersConverter jobParametersConverter;
116+
113117
SpringBootBatchConfiguration(DataSource dataSource, @BatchDataSource ObjectProvider<DataSource> batchDataSource,
114118
PlatformTransactionManager transactionManager,
115119
@BatchTransactionManager ObjectProvider<PlatformTransactionManager> batchTransactionManager,
116120
@BatchTaskExecutor ObjectProvider<TaskExecutor> batchTaskExecutor, BatchProperties properties,
117121
ObjectProvider<BatchConversionServiceCustomizer> batchConversionServiceCustomizers,
118-
ObjectProvider<ExecutionContextSerializer> executionContextSerializer) {
122+
ObjectProvider<ExecutionContextSerializer> executionContextSerializer,
123+
ObjectProvider<JobParametersConverter> jobParametersConverter) {
119124
this.dataSource = batchDataSource.getIfAvailable(() -> dataSource);
120125
this.transactionManager = batchTransactionManager.getIfAvailable(() -> transactionManager);
121126
this.taskExector = batchTaskExecutor.getIfAvailable();
122127
this.properties = properties;
123128
this.batchConversionServiceCustomizers = batchConversionServiceCustomizers.orderedStream().toList();
124129
this.executionContextSerializer = executionContextSerializer.getIfAvailable();
130+
this.jobParametersConverter = jobParametersConverter.getIfAvailable();
125131
}
126132

127133
@Override
@@ -161,6 +167,12 @@ protected ExecutionContextSerializer getExecutionContextSerializer() {
161167
: super.getExecutionContextSerializer();
162168
}
163169

170+
@Override
171+
protected JobParametersConverter getJobParametersConverter() {
172+
return (this.jobParametersConverter != null) ? this.jobParametersConverter
173+
: super.getJobParametersConverter();
174+
}
175+
164176
@Override
165177
protected TaskExecutor getTaskExecutor() {
166178
return (this.taskExector != null) ? this.taskExector : super.getTaskExecutor();

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfigurationTests.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@
3939
import org.springframework.batch.core.configuration.JobRegistry;
4040
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
4141
import org.springframework.batch.core.configuration.support.DefaultBatchConfiguration;
42+
import org.springframework.batch.core.converter.DefaultJobParametersConverter;
43+
import org.springframework.batch.core.converter.JobParametersConverter;
44+
import org.springframework.batch.core.converter.JsonJobParametersConverter;
4245
import org.springframework.batch.core.explore.JobExplorer;
4346
import org.springframework.batch.core.job.AbstractJob;
4447
import org.springframework.batch.core.launch.JobLauncher;
@@ -107,6 +110,7 @@
107110
* @author Mahmoud Ben Hassine
108111
* @author Lars Uffmann
109112
* @author Lasse Wulff
113+
* @author Yanming Zhou
110114
*/
111115
@ExtendWith(OutputCaptureExtension.class)
112116
class BatchAutoConfigurationTests {
@@ -520,6 +524,27 @@ void defaultExecutionContextSerializerIsUsed() {
520524
});
521525
}
522526

527+
@Test
528+
void customJobParametersConverterIsUsed() {
529+
this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class)
530+
.withBean(JobParametersConverter.class, JsonJobParametersConverter::new)
531+
.withPropertyValues("spring.datasource.generate-unique-name=true")
532+
.run((context) -> {
533+
assertThat(context).hasSingleBean(JsonJobParametersConverter.class);
534+
assertThat(context.getBean(SpringBootBatchConfiguration.class).getJobParametersConverter())
535+
.isInstanceOf(JsonJobParametersConverter.class);
536+
});
537+
}
538+
539+
@Test
540+
void defaultJobParametersConverterIsUsed() {
541+
this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class).run((context) -> {
542+
assertThat(context).doesNotHaveBean(JobParametersConverter.class);
543+
assertThat(context.getBean(SpringBootBatchConfiguration.class).getJobParametersConverter())
544+
.isInstanceOf(DefaultJobParametersConverter.class);
545+
});
546+
}
547+
523548
private JobLauncherApplicationRunner createInstance(String... registeredJobNames) {
524549
JobLauncherApplicationRunner runner = new JobLauncherApplicationRunner(mock(JobLauncher.class),
525550
mock(JobExplorer.class), mock(JobRepository.class));

0 commit comments

Comments
 (0)