Skip to content

Commit f0fe8bc

Browse files
committed
Merge pull request #34769 from EvaristeGalois11
* gh-34769: Polish "Add customizer for conversion service used by Spring Batch" Add customizer for conversion service used by Spring Batch Closes gh-34769
2 parents 60eb01b + f5e6547 commit f0fe8bc

File tree

3 files changed

+97
-2
lines changed

3 files changed

+97
-2
lines changed

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 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,6 +16,8 @@
1616

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

19+
import java.util.List;
20+
1921
import javax.sql.DataSource;
2022

2123
import org.springframework.batch.core.configuration.ListableJobLocator;
@@ -44,6 +46,7 @@
4446
import org.springframework.context.annotation.Conditional;
4547
import org.springframework.context.annotation.Configuration;
4648
import org.springframework.context.annotation.Import;
49+
import org.springframework.core.convert.support.ConfigurableConversionService;
4750
import org.springframework.jdbc.datasource.init.DatabasePopulator;
4851
import org.springframework.transaction.PlatformTransactionManager;
4952
import org.springframework.transaction.annotation.Isolation;
@@ -115,11 +118,15 @@ static class SpringBootBatchConfiguration extends DefaultBatchConfiguration {
115118

116119
private final BatchProperties properties;
117120

121+
private final List<BatchConversionServiceCustomizer> batchConversionServiceCustomizers;
122+
118123
SpringBootBatchConfiguration(DataSource dataSource, @BatchDataSource ObjectProvider<DataSource> batchDataSource,
119-
PlatformTransactionManager transactionManager, BatchProperties properties) {
124+
PlatformTransactionManager transactionManager, BatchProperties properties,
125+
ObjectProvider<BatchConversionServiceCustomizer> batchConversionServiceCustomizers) {
120126
this.dataSource = batchDataSource.getIfAvailable(() -> dataSource);
121127
this.transactionManager = transactionManager;
122128
this.properties = properties;
129+
this.batchConversionServiceCustomizers = batchConversionServiceCustomizers.orderedStream().toList();
123130
}
124131

125132
@Override
@@ -144,6 +151,15 @@ protected Isolation getIsolationLevelForCreate() {
144151
return (isolation != null) ? isolation : super.getIsolationLevelForCreate();
145152
}
146153

154+
@Override
155+
protected ConfigurableConversionService getConversionService() {
156+
ConfigurableConversionService conversionService = super.getConversionService();
157+
for (BatchConversionServiceCustomizer customizer : this.batchConversionServiceCustomizers) {
158+
customizer.customize(conversionService);
159+
}
160+
return conversionService;
161+
}
162+
147163
}
148164

149165
@Configuration(proxyBeanMethods = false)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2012-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.batch;
18+
19+
import org.springframework.batch.core.configuration.support.DefaultBatchConfiguration;
20+
import org.springframework.core.convert.support.ConfigurableConversionService;
21+
22+
/**
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.
27+
*
28+
* @author Claudio Nave
29+
* @since 3.1.0
30+
*/
31+
@FunctionalInterface
32+
public interface BatchConversionServiceCustomizer {
33+
34+
/**
35+
* Customize the {@link ConfigurableConversionService}.
36+
* @param configurableConversionService the ConfigurableConversionService to customize
37+
*/
38+
void customize(ConfigurableConversionService configurableConversionService);
39+
40+
}

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

Lines changed: 39 additions & 0 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,6 +73,8 @@
7173
import org.springframework.context.annotation.Bean;
7274
import org.springframework.context.annotation.Configuration;
7375
import org.springframework.context.annotation.Primary;
76+
import org.springframework.core.annotation.Order;
77+
import org.springframework.core.convert.support.ConfigurableConversionService;
7478
import org.springframework.jdbc.BadSqlGrammarException;
7579
import org.springframework.jdbc.core.JdbcTemplate;
7680
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
@@ -400,6 +404,24 @@ void whenTheUserDefinesTheirOwnDatabaseInitializerThenTheAutoConfiguredBatchInit
400404
.hasBean("customInitializer"));
401405
}
402406

407+
@Test
408+
void conversionServiceCustomizersAreCalled() {
409+
this.contextRunner.withUserConfiguration(TestConfiguration.class, EmbeddedDataSourceConfiguration.class)
410+
.withUserConfiguration(ConversionServiceCustomizersConfiguration.class)
411+
.run((context) -> {
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);
417+
ConfigurableConversionService configurableConversionService = context
418+
.getBean(SpringBootBatchConfiguration.class)
419+
.getConversionService();
420+
inOrder.verify(customizer).customize(configurableConversionService);
421+
inOrder.verify(anotherCustomizer).customize(configurableConversionService);
422+
});
423+
}
424+
403425
@Configuration(proxyBeanMethods = false)
404426
protected static class BatchDataSourceConfiguration {
405427

@@ -680,4 +702,21 @@ static class EnableBatchProcessingConfiguration {
680702

681703
}
682704

705+
@Configuration(proxyBeanMethods = false)
706+
static class ConversionServiceCustomizersConfiguration {
707+
708+
@Bean
709+
@Order(1)
710+
BatchConversionServiceCustomizer batchConversionServiceCustomizer() {
711+
return mock(BatchConversionServiceCustomizer.class);
712+
}
713+
714+
@Bean
715+
@Order(2)
716+
BatchConversionServiceCustomizer anotherBatchConversionServiceCustomizer() {
717+
return mock(BatchConversionServiceCustomizer.class);
718+
}
719+
720+
}
721+
683722
}

0 commit comments

Comments
 (0)