Skip to content

Commit cafa6f5

Browse files
EvaristeGalois11wilkinsona
authored andcommitted
Add customizer for conversion service used by Spring Batch
See gh-34769
1 parent 60eb01b commit cafa6f5

File tree

3 files changed

+102
-2
lines changed

3 files changed

+102
-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+
List<BatchConversionServiceCustomizer> batchConversionServiceCustomizers) {
120126
this.dataSource = batchDataSource.getIfAvailable(() -> dataSource);
121127
this.transactionManager = transactionManager;
122128
this.properties = properties;
129+
this.batchConversionServiceCustomizers = batchConversionServiceCustomizers;
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,39 @@
1+
/*
2+
* Copyright 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.core.convert.support.ConfigurableConversionService;
20+
21+
/**
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.
26+
*
27+
* @author Claudio Nave
28+
* @since 3.1.0
29+
*/
30+
@FunctionalInterface
31+
public interface BatchConversionServiceCustomizer {
32+
33+
/**
34+
* Customize the {@link ConfigurableConversionService}.
35+
* @param configurableConversionService the ConfigurableConversionService to customize
36+
*/
37+
void customize(ConfigurableConversionService configurableConversionService);
38+
39+
}

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@
7171
import org.springframework.context.annotation.Bean;
7272
import org.springframework.context.annotation.Configuration;
7373
import org.springframework.context.annotation.Primary;
74+
import org.springframework.core.convert.converter.Converter;
75+
import org.springframework.core.convert.support.ConfigurableConversionService;
7476
import org.springframework.jdbc.BadSqlGrammarException;
7577
import org.springframework.jdbc.core.JdbcTemplate;
7678
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
@@ -400,6 +402,21 @@ void whenTheUserDefinesTheirOwnDatabaseInitializerThenTheAutoConfiguredBatchInit
400402
.hasBean("customInitializer"));
401403
}
402404

405+
@Test
406+
void userProvidedCustomConverter() {
407+
this.contextRunner.withUserConfiguration(TestConfiguration.class, EmbeddedDataSourceConfiguration.class)
408+
.withUserConfiguration(RegisterCustomConverter.class)
409+
.run((context) -> {
410+
assertThat(context).hasSingleBean(SpringBootBatchConfiguration.class);
411+
ConfigurableConversionService configurableConversionService = context
412+
.getBean(SpringBootBatchConfiguration.class)
413+
.getConversionService();
414+
assertThat(configurableConversionService.canConvert(RegisterCustomConverter.Foo.class,
415+
RegisterCustomConverter.Bar.class))
416+
.isTrue();
417+
});
418+
}
419+
403420
@Configuration(proxyBeanMethods = false)
404421
protected static class BatchDataSourceConfiguration {
405422

@@ -680,4 +697,32 @@ static class EnableBatchProcessingConfiguration {
680697

681698
}
682699

700+
@Configuration(proxyBeanMethods = false)
701+
static class RegisterCustomConverter {
702+
703+
@Bean
704+
BatchConversionServiceCustomizer batchConversionServiceCustomizer() {
705+
return (configurableConversionService) -> configurableConversionService
706+
.addConverter(new FooToBarConverter());
707+
}
708+
709+
static class Foo {
710+
711+
}
712+
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+
724+
}
725+
726+
}
727+
683728
}

0 commit comments

Comments
 (0)