Add ability to customize the job parameters converter in the default batch configuration
Before this commit, it was impossible to customize the job parameters converter without overriding the entire`jobOperator()` method. This commit makes it possible to override `getJobParametersConverter()` or define a bean of type `JobParametersConverter` to customize the job parameters converter used by the job operator. Resolves #4650
This commit is contained in:
@@ -257,6 +257,12 @@ class BatchRegistrar implements ImportBeanDefinitionRegistrar {
|
||||
beanDefinitionBuilder.addPropertyReference("jobExplorer", "jobExplorer");
|
||||
beanDefinitionBuilder.addPropertyReference("jobRegistry", "jobRegistry");
|
||||
|
||||
// set optional properties
|
||||
String jobParametersConverterRef = batchAnnotation.jobParametersConverterRef();
|
||||
if (registry.containsBeanDefinition(jobParametersConverterRef)) {
|
||||
beanDefinitionBuilder.addPropertyReference("jobParametersConverter", jobParametersConverterRef);
|
||||
}
|
||||
|
||||
registry.registerBeanDefinition("jobOperator", beanDefinitionBuilder.getBeanDefinition());
|
||||
}
|
||||
|
||||
|
||||
@@ -28,6 +28,7 @@ import org.springframework.batch.core.configuration.JobRegistry;
|
||||
import org.springframework.batch.core.configuration.support.ApplicationContextFactory;
|
||||
import org.springframework.batch.core.configuration.support.AutomaticJobRegistrar;
|
||||
import org.springframework.batch.core.configuration.support.ScopeConfiguration;
|
||||
import org.springframework.batch.core.converter.JobParametersConverter;
|
||||
import org.springframework.batch.core.launch.JobLauncher;
|
||||
import org.springframework.batch.core.launch.support.TaskExecutorJobLauncher;
|
||||
import org.springframework.batch.core.repository.JobRepository;
|
||||
@@ -272,4 +273,11 @@ public @interface EnableBatchProcessing {
|
||||
*/
|
||||
String conversionServiceRef() default "conversionService";
|
||||
|
||||
/**
|
||||
* Set the {@link JobParametersConverter} to use in the job operator.
|
||||
* @return the bean name of the job parameters converter to use. Defaults to
|
||||
* {@literal jobParametersConverter}
|
||||
*/
|
||||
String jobParametersConverterRef() default "jobParametersConverter";
|
||||
|
||||
}
|
||||
|
||||
@@ -27,6 +27,8 @@ import org.springframework.batch.core.JobKeyGenerator;
|
||||
import org.springframework.batch.core.configuration.BatchConfigurationException;
|
||||
import org.springframework.batch.core.configuration.JobRegistry;
|
||||
import org.springframework.batch.core.converter.DateToStringConverter;
|
||||
import org.springframework.batch.core.converter.DefaultJobParametersConverter;
|
||||
import org.springframework.batch.core.converter.JobParametersConverter;
|
||||
import org.springframework.batch.core.converter.LocalDateTimeToStringConverter;
|
||||
import org.springframework.batch.core.converter.LocalDateToStringConverter;
|
||||
import org.springframework.batch.core.converter.LocalTimeToStringConverter;
|
||||
@@ -237,6 +239,7 @@ public class DefaultBatchConfiguration implements ApplicationContextAware {
|
||||
jobOperatorFactoryBean.setJobExplorer(jobExplorer);
|
||||
jobOperatorFactoryBean.setJobRegistry(jobRegistry);
|
||||
jobOperatorFactoryBean.setJobLauncher(jobLauncher);
|
||||
jobOperatorFactoryBean.setJobParametersConverter(getJobParametersConverter());
|
||||
try {
|
||||
jobOperatorFactoryBean.afterPropertiesSet();
|
||||
return jobOperatorFactoryBean.getObject();
|
||||
@@ -465,6 +468,15 @@ public class DefaultBatchConfiguration implements ApplicationContextAware {
|
||||
return new SyncTaskExecutor();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the {@link JobParametersConverter} to use in the job operator. Defaults to
|
||||
* {@link DefaultJobParametersConverter}
|
||||
* @return the {@link JobParametersConverter} to use in the job operator.
|
||||
*/
|
||||
protected JobParametersConverter getJobParametersConverter() {
|
||||
return new DefaultJobParametersConverter();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the conversion service to use in the job repository and job explorer. This
|
||||
* service is used to convert job parameters from String literal to typed values and
|
||||
|
||||
Reference in New Issue
Block a user