Skip to content

Auto-configure Spring Batch to use a JobsParametersConverter bean when available #44799

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.support.DefaultBatchConfiguration;
import org.springframework.batch.core.converter.JobParametersConverter;
import org.springframework.batch.core.explore.JobExplorer;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.ExecutionContextSerializer;
Expand Down Expand Up @@ -66,6 +67,7 @@
* @author Mahmoud Ben Hassine
* @author Lars Uffmann
* @author Lasse Wulff
* @author Yanming Zhou
* @since 1.0.0
*/
@AutoConfiguration(after = { HibernateJpaAutoConfiguration.class, TransactionAutoConfiguration.class })
Expand Down Expand Up @@ -110,18 +112,22 @@ static class SpringBootBatchConfiguration extends DefaultBatchConfiguration {

private final ExecutionContextSerializer executionContextSerializer;

private final JobParametersConverter jobParametersConverter;

SpringBootBatchConfiguration(DataSource dataSource, @BatchDataSource ObjectProvider<DataSource> batchDataSource,
PlatformTransactionManager transactionManager,
@BatchTransactionManager ObjectProvider<PlatformTransactionManager> batchTransactionManager,
@BatchTaskExecutor ObjectProvider<TaskExecutor> batchTaskExecutor, BatchProperties properties,
ObjectProvider<BatchConversionServiceCustomizer> batchConversionServiceCustomizers,
ObjectProvider<ExecutionContextSerializer> executionContextSerializer) {
ObjectProvider<ExecutionContextSerializer> executionContextSerializer,
ObjectProvider<JobParametersConverter> jobParametersConverter) {
this.dataSource = batchDataSource.getIfAvailable(() -> dataSource);
this.transactionManager = batchTransactionManager.getIfAvailable(() -> transactionManager);
this.taskExector = batchTaskExecutor.getIfAvailable();
this.properties = properties;
this.batchConversionServiceCustomizers = batchConversionServiceCustomizers.orderedStream().toList();
this.executionContextSerializer = executionContextSerializer.getIfAvailable();
this.jobParametersConverter = jobParametersConverter.getIfAvailable();
}

@Override
Expand Down Expand Up @@ -161,6 +167,12 @@ protected ExecutionContextSerializer getExecutionContextSerializer() {
: super.getExecutionContextSerializer();
}

@Override
protected JobParametersConverter getJobParametersConverter() {
return (this.jobParametersConverter != null) ? this.jobParametersConverter
: super.getJobParametersConverter();
}

@Override
protected TaskExecutor getTaskExecutor() {
return (this.taskExector != null) ? this.taskExector : super.getTaskExecutor();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
import org.springframework.batch.core.configuration.JobRegistry;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.support.DefaultBatchConfiguration;
import org.springframework.batch.core.converter.DefaultJobParametersConverter;
import org.springframework.batch.core.converter.JobParametersConverter;
import org.springframework.batch.core.converter.JsonJobParametersConverter;
import org.springframework.batch.core.explore.JobExplorer;
import org.springframework.batch.core.job.AbstractJob;
import org.springframework.batch.core.launch.JobLauncher;
Expand Down Expand Up @@ -107,6 +110,7 @@
* @author Mahmoud Ben Hassine
* @author Lars Uffmann
* @author Lasse Wulff
* @author Yanming Zhou
*/
@ExtendWith(OutputCaptureExtension.class)
class BatchAutoConfigurationTests {
Expand Down Expand Up @@ -520,6 +524,27 @@ void defaultExecutionContextSerializerIsUsed() {
});
}

@Test
void customJobParametersConverterIsUsed() {
this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class)
.withBean(JobParametersConverter.class, JsonJobParametersConverter::new)
.withPropertyValues("spring.datasource.generate-unique-name=true")
.run((context) -> {
assertThat(context).hasSingleBean(JsonJobParametersConverter.class);
assertThat(context.getBean(SpringBootBatchConfiguration.class).getJobParametersConverter())
.isInstanceOf(JsonJobParametersConverter.class);
});
}

@Test
void defaultJobParametersConverterIsUsed() {
this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class).run((context) -> {
assertThat(context).doesNotHaveBean(JobParametersConverter.class);
assertThat(context.getBean(SpringBootBatchConfiguration.class).getJobParametersConverter())
.isInstanceOf(DefaultJobParametersConverter.class);
});
}

private JobLauncherApplicationRunner createInstance(String... registeredJobNames) {
JobLauncherApplicationRunner runner = new JobLauncherApplicationRunner(mock(JobLauncher.class),
mock(JobExplorer.class), mock(JobRepository.class));
Expand Down