From cafa6f5d9cc879fb47667f44d504e2d1b479e775 Mon Sep 17 00:00:00 2001 From: Claudio Nave Date: Sun, 26 Mar 2023 17:02:28 +0200 Subject: [PATCH 1/2] 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 209b8bd187e..547cabf2842 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 00000000000..af3d3650825 --- /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 60e8c7d5dca..627ac5edf25 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; + } + + } + + } + } From f5e654748c8b23644ad3f70ba3202a3e1ea61d74 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Wed, 19 Apr 2023 14:15:08 +0100 Subject: [PATCH 2/2] Polish "Add customizer for conversion service used by Spring Batch" See gh-34769 --- .../batch/BatchAutoConfiguration.java | 4 +- .../BatchConversionServiceCustomizer.java | 11 ++--- .../batch/BatchAutoConfigurationTests.java | 44 ++++++++----------- 3 files changed, 27 insertions(+), 32 deletions(-) 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 547cabf2842..3c050a0856b 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 @@ -122,11 +122,11 @@ public class BatchAutoConfiguration { SpringBootBatchConfiguration(DataSource dataSource, @BatchDataSource ObjectProvider batchDataSource, PlatformTransactionManager transactionManager, BatchProperties properties, - List batchConversionServiceCustomizers) { + ObjectProvider batchConversionServiceCustomizers) { this.dataSource = batchDataSource.getIfAvailable(() -> dataSource); this.transactionManager = transactionManager; this.properties = properties; - this.batchConversionServiceCustomizers = batchConversionServiceCustomizers; + this.batchConversionServiceCustomizers = batchConversionServiceCustomizers.orderedStream().toList(); } @Override 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 index af3d3650825..a75a9a30d8a 100644 --- 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 @@ -1,5 +1,5 @@ /* - * Copyright 2023 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,13 +16,14 @@ package org.springframework.boot.autoconfigure.batch; +import org.springframework.batch.core.configuration.support.DefaultBatchConfiguration; 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. + * Callback interface that can be implemented by beans wishing to customize the + * {@link ConfigurableConversionService} that is + * {@link DefaultBatchConfiguration#getConversionService provided by + * DefaultBatchAutoConfiguration} while retaining its default auto-configuration. * * @author Claudio Nave * @since 3.1.0 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 627ac5edf25..19a02f1f1d3 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 @@ -24,6 +24,8 @@ import javax.sql.DataSource; import jakarta.persistence.EntityManagerFactory; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InOrder; +import org.mockito.Mockito; import org.springframework.batch.core.BatchStatus; import org.springframework.batch.core.Job; @@ -71,7 +73,7 @@ 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.annotation.Order; import org.springframework.core.convert.support.ConfigurableConversionService; import org.springframework.jdbc.BadSqlGrammarException; import org.springframework.jdbc.core.JdbcTemplate; @@ -403,17 +405,20 @@ class BatchAutoConfigurationTests { } @Test - void userProvidedCustomConverter() { + void conversionServiceCustomizersAreCalled() { this.contextRunner.withUserConfiguration(TestConfiguration.class, EmbeddedDataSourceConfiguration.class) - .withUserConfiguration(RegisterCustomConverter.class) + .withUserConfiguration(ConversionServiceCustomizersConfiguration.class) .run((context) -> { - assertThat(context).hasSingleBean(SpringBootBatchConfiguration.class); + BatchConversionServiceCustomizer customizer = context.getBean("batchConversionServiceCustomizer", + BatchConversionServiceCustomizer.class); + BatchConversionServiceCustomizer anotherCustomizer = context + .getBean("anotherBatchConversionServiceCustomizer", BatchConversionServiceCustomizer.class); + InOrder inOrder = Mockito.inOrder(customizer, anotherCustomizer); ConfigurableConversionService configurableConversionService = context .getBean(SpringBootBatchConfiguration.class) .getConversionService(); - assertThat(configurableConversionService.canConvert(RegisterCustomConverter.Foo.class, - RegisterCustomConverter.Bar.class)) - .isTrue(); + inOrder.verify(customizer).customize(configurableConversionService); + inOrder.verify(anotherCustomizer).customize(configurableConversionService); }); } @@ -698,29 +703,18 @@ class BatchAutoConfigurationTests { } @Configuration(proxyBeanMethods = false) - static class RegisterCustomConverter { + static class ConversionServiceCustomizersConfiguration { @Bean + @Order(1) BatchConversionServiceCustomizer batchConversionServiceCustomizer() { - return (configurableConversionService) -> configurableConversionService - .addConverter(new FooToBarConverter()); - } - - static class Foo { - + return mock(BatchConversionServiceCustomizer.class); } - static class Bar { - - } - - static class FooToBarConverter implements Converter { - - @Override - public Bar convert(Foo source) { - return null; - } - + @Bean + @Order(2) + BatchConversionServiceCustomizer anotherBatchConversionServiceCustomizer() { + return mock(BatchConversionServiceCustomizer.class); } }