From cafa6f5d9cc879fb47667f44d504e2d1b479e775 Mon Sep 17 00:00:00 2001 From: Claudio Nave Date: Sun, 26 Mar 2023 17:02:28 +0200 Subject: [PATCH] Add customizer for conversion service used by Spring Batch See gh-34769 --- .../batch/BatchAutoConfiguration.java | 20 ++++++++- .../BatchConversionServiceCustomizer.java | 39 ++++++++++++++++ .../batch/BatchAutoConfigurationTests.java | 45 +++++++++++++++++++ 3 files changed, 102 insertions(+), 2 deletions(-) create mode 100644 spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchConversionServiceCustomizer.java diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfiguration.java index 209b8bd187..547cabf284 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2022 the original author or authors. + * Copyright 2012-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,6 +16,8 @@ package org.springframework.boot.autoconfigure.batch; +import java.util.List; + import javax.sql.DataSource; import org.springframework.batch.core.configuration.ListableJobLocator; @@ -44,6 +46,7 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; +import org.springframework.core.convert.support.ConfigurableConversionService; import org.springframework.jdbc.datasource.init.DatabasePopulator; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.annotation.Isolation; @@ -115,11 +118,15 @@ public class BatchAutoConfiguration { private final BatchProperties properties; + private final List batchConversionServiceCustomizers; + SpringBootBatchConfiguration(DataSource dataSource, @BatchDataSource ObjectProvider batchDataSource, - PlatformTransactionManager transactionManager, BatchProperties properties) { + PlatformTransactionManager transactionManager, BatchProperties properties, + List batchConversionServiceCustomizers) { this.dataSource = batchDataSource.getIfAvailable(() -> dataSource); this.transactionManager = transactionManager; this.properties = properties; + this.batchConversionServiceCustomizers = batchConversionServiceCustomizers; } @Override @@ -144,6 +151,15 @@ public class BatchAutoConfiguration { return (isolation != null) ? isolation : super.getIsolationLevelForCreate(); } + @Override + protected ConfigurableConversionService getConversionService() { + ConfigurableConversionService conversionService = super.getConversionService(); + for (BatchConversionServiceCustomizer customizer : this.batchConversionServiceCustomizers) { + customizer.customize(conversionService); + } + return conversionService; + } + } @Configuration(proxyBeanMethods = false) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchConversionServiceCustomizer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchConversionServiceCustomizer.java new file mode 100644 index 0000000000..af3d365082 --- /dev/null +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchConversionServiceCustomizer.java @@ -0,0 +1,39 @@ +/* + * Copyright 2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.autoconfigure.batch; + +import org.springframework.core.convert.support.ConfigurableConversionService; + +/** + * Callback interface that can be implemented by beans wishing to further customize the + * {@link ConfigurableConversionService} used in + * {@link org.springframework.batch.core.configuration.support.DefaultBatchConfiguration} + * retaining its default auto-configuration. + * + * @author Claudio Nave + * @since 3.1.0 + */ +@FunctionalInterface +public interface BatchConversionServiceCustomizer { + + /** + * Customize the {@link ConfigurableConversionService}. + * @param configurableConversionService the ConfigurableConversionService to customize + */ + void customize(ConfigurableConversionService configurableConversionService); + +} diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfigurationTests.java index 60e8c7d5dc..627ac5edf2 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/batch/BatchAutoConfigurationTests.java @@ -71,6 +71,8 @@ import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; +import org.springframework.core.convert.converter.Converter; +import org.springframework.core.convert.support.ConfigurableConversionService; import org.springframework.jdbc.BadSqlGrammarException; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.datasource.DataSourceTransactionManager; @@ -400,6 +402,21 @@ class BatchAutoConfigurationTests { .hasBean("customInitializer")); } + @Test + void userProvidedCustomConverter() { + this.contextRunner.withUserConfiguration(TestConfiguration.class, EmbeddedDataSourceConfiguration.class) + .withUserConfiguration(RegisterCustomConverter.class) + .run((context) -> { + assertThat(context).hasSingleBean(SpringBootBatchConfiguration.class); + ConfigurableConversionService configurableConversionService = context + .getBean(SpringBootBatchConfiguration.class) + .getConversionService(); + assertThat(configurableConversionService.canConvert(RegisterCustomConverter.Foo.class, + RegisterCustomConverter.Bar.class)) + .isTrue(); + }); + } + @Configuration(proxyBeanMethods = false) protected static class BatchDataSourceConfiguration { @@ -680,4 +697,32 @@ class BatchAutoConfigurationTests { } + @Configuration(proxyBeanMethods = false) + static class RegisterCustomConverter { + + @Bean + BatchConversionServiceCustomizer batchConversionServiceCustomizer() { + return (configurableConversionService) -> configurableConversionService + .addConverter(new FooToBarConverter()); + } + + static class Foo { + + } + + static class Bar { + + } + + static class FooToBarConverter implements Converter { + + @Override + public Bar convert(Foo source) { + return null; + } + + } + + } + }