Skip to content

Commit f5e6547

Browse files
committed
Polish "Add customizer for conversion service used by Spring Batch"
See gh-34769
1 parent cafa6f5 commit f5e6547

File tree

3 files changed

+27
-32
lines changed

3 files changed

+27
-32
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,11 @@ static class SpringBootBatchConfiguration extends DefaultBatchConfiguration {
122122

123123
SpringBootBatchConfiguration(DataSource dataSource, @BatchDataSource ObjectProvider<DataSource> batchDataSource,
124124
PlatformTransactionManager transactionManager, BatchProperties properties,
125-
List<BatchConversionServiceCustomizer> batchConversionServiceCustomizers) {
125+
ObjectProvider<BatchConversionServiceCustomizer> batchConversionServiceCustomizers) {
126126
this.dataSource = batchDataSource.getIfAvailable(() -> dataSource);
127127
this.transactionManager = transactionManager;
128128
this.properties = properties;
129-
this.batchConversionServiceCustomizers = batchConversionServiceCustomizers;
129+
this.batchConversionServiceCustomizers = batchConversionServiceCustomizers.orderedStream().toList();
130130
}
131131

132132
@Override

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023 the original author or authors.
2+
* Copyright 2012-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,13 +16,14 @@
1616

1717
package org.springframework.boot.autoconfigure.batch;
1818

19+
import org.springframework.batch.core.configuration.support.DefaultBatchConfiguration;
1920
import org.springframework.core.convert.support.ConfigurableConversionService;
2021

2122
/**
22-
* Callback interface that can be implemented by beans wishing to further customize the
23-
* {@link ConfigurableConversionService} used in
24-
* {@link org.springframework.batch.core.configuration.support.DefaultBatchConfiguration}
25-
* retaining its default auto-configuration.
23+
* Callback interface that can be implemented by beans wishing to customize the
24+
* {@link ConfigurableConversionService} that is
25+
* {@link DefaultBatchConfiguration#getConversionService provided by
26+
* DefaultBatchAutoConfiguration} while retaining its default auto-configuration.
2627
*
2728
* @author Claudio Nave
2829
* @since 3.1.0

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

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import jakarta.persistence.EntityManagerFactory;
2525
import org.junit.jupiter.api.Test;
2626
import org.junit.jupiter.api.extension.ExtendWith;
27+
import org.mockito.InOrder;
28+
import org.mockito.Mockito;
2729

2830
import org.springframework.batch.core.BatchStatus;
2931
import org.springframework.batch.core.Job;
@@ -71,7 +73,7 @@
7173
import org.springframework.context.annotation.Bean;
7274
import org.springframework.context.annotation.Configuration;
7375
import org.springframework.context.annotation.Primary;
74-
import org.springframework.core.convert.converter.Converter;
76+
import org.springframework.core.annotation.Order;
7577
import org.springframework.core.convert.support.ConfigurableConversionService;
7678
import org.springframework.jdbc.BadSqlGrammarException;
7779
import org.springframework.jdbc.core.JdbcTemplate;
@@ -403,17 +405,20 @@ void whenTheUserDefinesTheirOwnDatabaseInitializerThenTheAutoConfiguredBatchInit
403405
}
404406

405407
@Test
406-
void userProvidedCustomConverter() {
408+
void conversionServiceCustomizersAreCalled() {
407409
this.contextRunner.withUserConfiguration(TestConfiguration.class, EmbeddedDataSourceConfiguration.class)
408-
.withUserConfiguration(RegisterCustomConverter.class)
410+
.withUserConfiguration(ConversionServiceCustomizersConfiguration.class)
409411
.run((context) -> {
410-
assertThat(context).hasSingleBean(SpringBootBatchConfiguration.class);
412+
BatchConversionServiceCustomizer customizer = context.getBean("batchConversionServiceCustomizer",
413+
BatchConversionServiceCustomizer.class);
414+
BatchConversionServiceCustomizer anotherCustomizer = context
415+
.getBean("anotherBatchConversionServiceCustomizer", BatchConversionServiceCustomizer.class);
416+
InOrder inOrder = Mockito.inOrder(customizer, anotherCustomizer);
411417
ConfigurableConversionService configurableConversionService = context
412418
.getBean(SpringBootBatchConfiguration.class)
413419
.getConversionService();
414-
assertThat(configurableConversionService.canConvert(RegisterCustomConverter.Foo.class,
415-
RegisterCustomConverter.Bar.class))
416-
.isTrue();
420+
inOrder.verify(customizer).customize(configurableConversionService);
421+
inOrder.verify(anotherCustomizer).customize(configurableConversionService);
417422
});
418423
}
419424

@@ -698,29 +703,18 @@ static class EnableBatchProcessingConfiguration {
698703
}
699704

700705
@Configuration(proxyBeanMethods = false)
701-
static class RegisterCustomConverter {
706+
static class ConversionServiceCustomizersConfiguration {
702707

703708
@Bean
709+
@Order(1)
704710
BatchConversionServiceCustomizer batchConversionServiceCustomizer() {
705-
return (configurableConversionService) -> configurableConversionService
706-
.addConverter(new FooToBarConverter());
707-
}
708-
709-
static class Foo {
710-
711+
return mock(BatchConversionServiceCustomizer.class);
711712
}
712713

713-
static class Bar {
714-
715-
}
716-
717-
static class FooToBarConverter implements Converter<Foo, Bar> {
718-
719-
@Override
720-
public Bar convert(Foo source) {
721-
return null;
722-
}
723-
714+
@Bean
715+
@Order(2)
716+
BatchConversionServiceCustomizer anotherBatchConversionServiceCustomizer() {
717+
return mock(BatchConversionServiceCustomizer.class);
724718
}
725719

726720
}

0 commit comments

Comments
 (0)