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..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 @@ -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, + ObjectProvider batchConversionServiceCustomizers) { this.dataSource = batchDataSource.getIfAvailable(() -> dataSource); this.transactionManager = transactionManager; this.properties = properties; + this.batchConversionServiceCustomizers = batchConversionServiceCustomizers.orderedStream().toList(); } @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..a75a9a30d8a --- /dev/null +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/batch/BatchConversionServiceCustomizer.java @@ -0,0 +1,40 @@ +/* + * 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. + * 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.batch.core.configuration.support.DefaultBatchConfiguration; +import org.springframework.core.convert.support.ConfigurableConversionService; + +/** + * 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 + */ +@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..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,6 +73,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.annotation.Order; +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 +404,24 @@ class BatchAutoConfigurationTests { .hasBean("customInitializer")); } + @Test + void conversionServiceCustomizersAreCalled() { + this.contextRunner.withUserConfiguration(TestConfiguration.class, EmbeddedDataSourceConfiguration.class) + .withUserConfiguration(ConversionServiceCustomizersConfiguration.class) + .run((context) -> { + 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(); + inOrder.verify(customizer).customize(configurableConversionService); + inOrder.verify(anotherCustomizer).customize(configurableConversionService); + }); + } + @Configuration(proxyBeanMethods = false) protected static class BatchDataSourceConfiguration { @@ -680,4 +702,21 @@ class BatchAutoConfigurationTests { } + @Configuration(proxyBeanMethods = false) + static class ConversionServiceCustomizersConfiguration { + + @Bean + @Order(1) + BatchConversionServiceCustomizer batchConversionServiceCustomizer() { + return mock(BatchConversionServiceCustomizer.class); + } + + @Bean + @Order(2) + BatchConversionServiceCustomizer anotherBatchConversionServiceCustomizer() { + return mock(BatchConversionServiceCustomizer.class); + } + + } + }